2010-01-11 68 views
3

我正在創建僅適用於System-Tray的應用程序。有沒有主窗體的圖標有點複雜,但通過StackOverflow上的以前的主題我已經完成了。右鍵單擊工作正常,我已經在上下文菜單中鏈接,等等。系統托盤notifyIcon不接受左鍵單擊事件

我遇到了左鍵單擊的問題。據我所知,「notifyIcon1_Click」事件根本沒有觸發。

private void notifyIcon1_Click(object sender, EventArgs e) 
    { 
     Debug.WriteLine("Does it work here?"); 

     if (e.Equals(MouseButtons.Left)) 
     { 
      Debug.WriteLine("It worked!"); 
     } 
    } 

無論那些調試行的輸出是在這種情況下不停止程序,斷點等

我這樣做是否有誤?我的下一步應該是什麼?我在C#中編寫了這個代碼,如果這對於任務欄行爲來說很重要的話,那麼可以使用Windows 7。

+2

你能告訴您更多的代碼?事件是否「有線」? – 2010-01-11 22:41:18

+3

在Designer.cs文件中正確連接了事件 - 「this.notifyIcon1.Click + = new System.EventHandler(this.notifyIcon1_Click);'? – ChrisF 2010-01-11 22:41:38

+2

你確實記得實際上將它註冊爲事件處理程序,對吧? – 2010-01-11 22:41:41

回答

6

如果您想確定是左鍵還是右鍵單擊,請連接MouseClick,而不是單擊。

這樣,你得到這樣的簽名:

private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) 
{ 
    if(e.Button == MouseButtons.Left) 
     //Do the awesome left clickness 
    else if (e.Button == MouseButtons.Right) 
     //Do the wickedy right clickness 
    else 
     //Some other button from the enum :) 
} 
+0

相當於「this.notifyIcon1.Click + = new System.EventHandler(this.notifyIcon1_Click)」;在這種情況下?顯然(即用「MouseClick」替換「Click」會拋出「沒有超載'notifyIcon1_MouseClick'與委託'System.EventHandler''錯誤匹配。」 除此之外,謝謝!我即將開始討論爲什麼 – cksubs 2010-01-12 00:09:43

+0

*編輯,試圖獲得上述評論者使用的代碼塊,但它沒有工作,希望評論有一個全功能的文本編輯器。 .. – cksubs 2010-01-12 00:15:46

+0

沒關係,明白了!謝謝! – cksubs 2010-01-12 00:33:19