About Me

My Photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Tuesday, October 02, 2007

Implementing Custom ToolTip in C#

I needed to change the default font family, of the tool-tip, to a Unicode version; since it doesn't accept Japanese characters.

Here is my tool tip class, which inherent:

    1    class WaterMarkToolTip : ToolTip

    2     {

    3         public WaterMarkToolTip()

    4         {

    5             this.OwnerDraw = true;

    6             this.Draw += new DrawToolTipEventHandler(OnDraw);

    7 

    8         }

    9 

   10         public WaterMarkToolTip(System.ComponentModel.IContainer Cont)

   11         {

   12             this.OwnerDraw = true;

   13             this.Draw += new DrawToolTipEventHandler(OnDraw);

   14         }

   15 

   16         private void OnDraw(object sender, DrawToolTipEventArgs e)

   17         {

   18             DrawToolTipEventArgs newArgs = new DrawToolTipEventArgs(e.Graphics,

   19                 e.AssociatedWindow, e.AssociatedControl, e.Bounds, e.ToolTipText,

   20                 this.BackColor, this.ForeColor, new Font("Arial Unicode MS", 8.25f, FontStyle.Bold));

   21             newArgs.DrawBackground();

   22             newArgs.DrawBorder();

   23             newArgs.DrawText(TextFormatFlags.TextBoxControl);

   24         }

   25     }



Here is the implementation into the toolstripbutton:



    1     class WaterMarkToolStripButton : ToolStripButton

    2     {

    3         private WaterMarkToolTip toolTip;

    4         private string _toolTipText;

    5 

    6         public WaterMarkToolStripButton()

    7         {

    8             this.toolTip = new WaterMarkToolTip();

    9             this.toolTip.UseAnimation = true;

   10             this.toolTip.AutoPopDelay = 500;

   11             this.toolTip.AutomaticDelay = 500;

   12 

   13             this.MouseHover += new EventHandler(WaterMarkToolStrip_MouseHover);

   14 

   15         }

   16 

   17         void WaterMarkToolStrip_MouseHover(object sender, EventArgs e)

   18         {

   19             this.AutoToolTip = false;

   20 

   21             this.toolTip.Show(this._toolTipText, Owner, 1500 );

   22         }

   23 

   24         /// <summary>

   25         /// Get/Set ToolTipText for this control

   26         /// </summary>

   27         public new string WaterMarkToolTipText

   28         {

   29             get

   30             {

   31                 return this._toolTipText;

   32             }

   33             set

   34             {

   35                 this.toolTip.RemoveAll();

   36                 this._toolTipText = " " + value + " ";

   37             }

   38         }

   39 

   40     }

2 comments:

slide said...

If you are subclassing the ToolTop control, why not just override the event items:

protected override void OnDraw(DrawToolTipEventArgs e) {
base.OnDraw(e);

// your code here
}

and for the ToolStripButton:

protected override void OnMouseHover(EventArgs e) {
base.OnMouseHover(e);

// your code here
}

this is actually faster and the recommended way from MS (even through their IDE doesn't do that).

Anonymous said...

Customize the schedule tooltip in C#.NET