我正在寻找一种向.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     }

其他提示

您可以使用自己的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样式使用应用程序清单

添加新的<!>“应用程序清单文件<!>”;并在<!> lt; assembly <!> gt下添加以下内容;元素:

<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类的实现中覆盖CreateParams方法... 即。

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

           return cp;
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top