【问题标题】:Add close button (red x) to a .NET ToolTip将关闭按钮(红色 x)添加到 .NET 工具提示
【发布时间】:2008-10-28 22:03:51
【问题描述】:
我正在寻找一种将关闭按钮添加到 .NET ToolTip 对象的方法,该对象类似于 NotifyIcon 的对象。我将工具提示用作使用 Show() 方法以编程方式调用的消息气球。这很好用,但没有 onclick 事件或关闭工具提示的简单方法。您必须在代码中的其他地方调用 Hide() 方法,我希望工具提示能够自行关闭。我知道网络上有几个气球工具提示,它们使用管理和非托管代码通过 Windows API 执行此操作,但我宁愿呆在我舒适的 .NET 世界中。我有一个调用我的 .NET 应用程序的第三方应用程序,它在尝试显示非托管工具提示时崩溃。
【问题讨论】:
标签:
c#
.net
winforms
tooltip
【解决方案1】:
您可以尝试通过覆盖现有的工具提示窗口并自定义 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 }
【解决方案2】:
您可以使用自己的设置 TTS_CLOSE 样式的 CreateParams 子类化 ToolTip 类:
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 样式也是requires TTS_BALLOON 样式,您还必须在工具提示上设置 ToolTipTitle 属性。
要使此样式生效,您需要启用 Common Controls v6 样式using an application manifest。
添加一个新的“应用程序清单文件”并在 元素下添加以下内容:
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
至少在 Visual Studio 2012 中,这些内容包含在默认模板中,但已被注释掉 - 您可以取消注释。
【解决方案3】:
您可以尝试在 ToolTip 类的实现中覆盖 CreateParams 方法...
即
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style = 0x80 | 0x40; //TTS_BALLOON & TTS_CLOSE
return cp;
}
}