- Labelコントロール
- ToolTipコントロール
{ InitializeComponent(); // Make the OwnerDraw enabled this.toolTip1.OwnerDraw = true; this.toolTip1.Draw += new DrawToolTipEventHandler(this.toolTip1_Draw); this.toolTip1.Popup += new PopupEventHandler(this.toolTip1_Popup); }
// Handles drawing the ToolTip. private void toolTip1_Draw(System.Object sender, System.Windows.Forms.DrawToolTipEventArgs e) { if (e.AssociatedControl.Tag != null) { e.DrawBackground(); // Create assembly System.Reflection.Assembly assembly; assembly = System.Reflection.Assembly.GetExecutingAssembly(); // Create ResourceManager System.Resources.ResourceManager rm = new System.Resources.ResourceManager(“ToolTipOwnerDraw.Properties.Resources”, assembly); // Get the image from the resources Image image = (Image)rm.GetObject(e.AssociatedControl.Tag.ToString()); // Create point for upper-left corner of image. float posX = 0; if (this.size.Width > image.Width) posX = (this.size.Width – image.Width) / 2; PointF upperLeft = new PointF(posX, this.size.Height-10); // Draw image to screen. e.Graphics.DrawImage(image, upperLeft); // Draw the standard border. e.DrawBorder(); // Draw the custom text. // The using block will dispose the StringFormat automatically. using (StringFormat sf = new StringFormat()) { sf.Alignment = StringAlignment.Near; sf.LineAlignment = StringAlignment.Center; sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None; sf.FormatFlags = StringFormatFlags.NoWrap; Rectangle rect = new Rectangle(new Point(PADDING/2, 0), this.size); e.Graphics.DrawString(e.ToolTipText, e.Font, SystemBrushes.ActiveCaptionText, rect, sf); } } else { // Draw the ToolTip using default values e.DrawBackground(); e.DrawBorder(); e.DrawText(); } }