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

2019-04-19 20:55发布

问题:

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.

回答1:

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);
    7 
    8         }
    9 
   10         public MyToolTip(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         {
                      ...Code Stuff...
   24         }
   25     }


回答2:

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
{
    get
    {
       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:

<dependency>
  <dependentAssembly>
    <assemblyIdentity
        type="win32"
        name="Microsoft.Windows.Common-Controls"
        version="6.0.0.0"
        processorArchitecture="*"
        publicKeyToken="6595b64144ccf1df"
        language="*"
      />
  </dependentAssembly>
</dependency> 

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



回答3:

You can try to override CreateParams method in your implementation of ToolTip class... i.e.

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

           return cp;
        }
    }