using System.Drawing; using System.Windows.Forms; using System; namespace CustomToolTipTest { public partial class CustomToolTipTestForm : Form { private Font m_font = new Font("Arial", 15.5F); private Button button1; private ToolTip m_toolTip; private System.ComponentModel.IContainer components = null; public CustomToolTipTestForm() { InitializeComponent(); this.m_toolTip.Popup += new PopupEventHandler(toolTip1_Popup); this.m_toolTip.Draw += new DrawToolTipEventHandler(toolTip1_Draw); this.m_toolTip.OwnerDraw = true; } void toolTip1_Popup(object sender, PopupEventArgs e) { SizeF size = e.AssociatedControl.CreateGraphics().MeasureString(m_toolTip.GetToolTip(e.AssociatedControl), m_font); e.ToolTipSize = new Size(Convert.ToInt32(size.Width), Convert.ToInt32(size.Height)); } void toolTip1_Draw(object sender, DrawToolTipEventArgs e) { DrawToolTipEventArgs newArgs = new DrawToolTipEventArgs(e.Graphics, e.AssociatedWindow, e.AssociatedControl, e.Bounds, e.ToolTipText, m_toolTip.BackColor, m_toolTip.ForeColor, m_font); newArgs.DrawBackground(); newArgs.DrawBorder(); newArgs.DrawText(TextFormatFlags.TextBoxControl); } private void button1_Click(object sender, EventArgs e) { Application.Exit(); } protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } this.m_toolTip.Popup -= new PopupEventHandler(toolTip1_Popup); this.m_toolTip.Draw -= new DrawToolTipEventHandler(toolTip1_Draw); base.Dispose(disposing); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.button1 = new System.Windows.Forms.Button(); this.m_toolTip = new System.Windows.Forms.ToolTip(this.components); this.SuspendLayout(); this.button1.Location = new System.Drawing.Point(1, 3); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "Exit"; this.m_toolTip.SetToolTip(this.button1, "click to exit"); this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Name = "CustomToolTipTestForm"; this.Text = "CustomToolTipTestForm"; this.ResumeLayout(false); } } static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new CustomToolTipTestForm()); } } }