2008-10-28 51 views
12

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

回答

4

您可以通过覆盖现有工具并自定义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

你可以尝试覆盖的CreateParams方法在您的实现工具提示类的...... 即

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

      return cp; 
     } 
    } 
+0

任何人都可以证实此操作吗?它是什么样子的? – Justin 2011-02-11 17:46:51

2

你也可以继承你自己的CreateParams工具提示类设置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样式也是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,至少,这个东西包含在默认的模板,但注释掉 - 你可以取消注释。

Tooltip with close button