Add close button (red x) to a .NET ToolTip

Posted on

Problem :

I’m looking for a way to add a close button to a .NET ToolTip object similar to the one the NotifyIcon has. I’m using the tooltip as a message balloon called programatically with the Show() method. That works fine but there is no onclick event or easy way to close the tooltip. You have to call the Hide() method somewhere else in your code and I would rather have the tooltip be able to close itself. I know there are several balloon tooltips around the net that use manage and unmanaged code to perform this with the windows API, but I would rather stay in my comfy .NET world. I have a thrid party application that calls my .NET application and it has crashes when trying to display unmanaged tooltips.

Solution :

You could try an implement your own tool tip window by overriding the existing one and customizing the onDraw function. I never tried adding a button, but have done other customizations with the tooltip before.

    1    class MyToolTip : ToolTip
    2     {
    3         public MyToolTip()
    4         {
    5             this.OwnerDraw = true;
    6             this.Draw += new DrawToolTipEventHandler(OnDraw);
    8         }
   10         public MyToolTip(System.ComponentModel.IContainer Cont)
   11         {
   12             this.OwnerDraw = true;
   13             this.Draw += new DrawToolTipEventHandler(OnDraw);
   14         }
   16         private void OnDraw(object sender, DrawToolTipEventArgs e)
   17         {
                      ...Code Stuff...
   24         }
   25     }

You can subclass the ToolTip class with your own CreateParams that sets the TTS_CLOSE style:

private const int TTS_BALLOON = 0x80;
private const int TTS_CLOSE = 0x40;
protected override CreateParams CreateParams
       var cp = base.CreateParams;
       cp.Style = TTS_BALLOON | TTS_CLOSE;
       return cp;

The TTS_CLOSE style also requires the TTS_BALLOON style and you must also set the ToolTipTitle property on the tooltip.

To get this style to work, you need to enable the Common Controls v6 styles using an application manifest.

Add a new “Application Manifest File” and add the following under the <assembly> element:


In Visual Studio 2012, at least, this stuff is included in the default template but commented out – you can just uncomment it.

Tooltip with close button

You can try to override CreateParams method in your implementation of ToolTip class…

    protected override CreateParams CreateParams
           CreateParams cp = base.CreateParams;
           cp.Style = 0x80 | 0x40; //TTS_BALLOON & TTS_CLOSE

           return cp;

Leave a Reply

Your email address will not be published. Required fields are marked *