2008-10-28 53 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