簡體   English   中英

系統托盤通知圖標不會接受左鍵單擊事件

[英]System Tray notifyIcon won't accept left-click event

我正在創建一個僅限系統托盤的應用程序。 沒有主窗體的圖標有點復雜,但通過以前關於 StackOverflow 的主題,我已經解決了。 右鍵單擊工作正常,我已經鏈接到上下文菜單等。

我在左鍵單擊時遇到問題。 據我所知,“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。

如果要確定是左鍵單擊還是右鍵單擊,請連接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 :)
}

如果你想要消息/氣球本身的點擊事件使用

_notifyIcon.BalloonTipClicked += notifyIconBalloon_Click;


private void notifyIconBalloon_Click(object sender, EventArgs e)
{
// your code
}
    private void NotifyIcon_Click(object sender, EventArgs e)
    {
        MouseEventArgs mouseEventArgs = (MouseEventArgs)e;

        if (mouseEventArgs.Button == MouseButtons.Right && IsHandleCreated)
        {
            popupMenu1.ShowPopup(MousePosition);
            return;
        }

        if (mouseEventArgs.Button == MouseButtons.Left && IsHandleCreated)
        {
            if (isWindowMinimized)
                showWindow();
            else
                hideWindow();
        }
    }

另一個答案不清楚您需要 MouseClick 事件而不是 Click。

notifyIcon.MouseClick += MyClickHandler;

然后您的處理程序函數將正常工作。

void MyClickHandler(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        Console.WriteLine("Left click!");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM