将关闭按钮(红色x)添加到.NET工具提示

我正在寻找一种方法来向.NET ToolTip对象添加一个类似于NotifyIcon的关闭按钮。 我正在使用工具提示作为使用Show()方法以编程方式调用的消息气球。 这工作正常,但没有onclick事件或关闭工具提示的简单方法。 你必须在代码中的其他地方调用Hide()方法,我宁愿让工具提示能够关闭它自己。 我知道网络周围有几个使用管理和非托管代码的气球工具提示来执行Windows API,但我宁愿留在我舒适的.NET世界中。 我有一个第三方应用程序调用我的.NET应用程序,它在尝试显示非托管工具提示时崩溃。

您可以通过覆盖现有工具提示窗口并自定义onDraw函数来尝试实现自己的工具提示窗口。 我从未尝试过添加按钮,但之前使用工具提示进行了其他自定义。

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 } 

您可以尝试在ToolTip类的实现中覆盖CreateParams方法…即

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

您可以使用自己的CreateParams将ToolTip类子类化,以设置TTS_CLOSE样式:

 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; } } 

TTS_CLOSE样式还需要 TTS_BALLOON样式,您还必须在工具提示上设置ToolTipTitle属性。

要使此样式起作用,您需要使用应用程序清单启用Common Controls v6样式。

添加一个新的“Application Manifest File”并在元素下添加以下内容:

      

至少在Visual Studio 2012中,这些东西包含在默认模板中但已注释掉 – 您可以取消注释它。

工具提示与关闭按钮