簡體   English   中英

在NotifyIcon上下文菜單中單擊ContextMenuItem會調用NotifyIcon click事件

[英]Clicking a ContextMenuItem in a NotifyIcon Context Menu calls the NotifyIcon click Event

我有一個僅在托盤中啟動的WinForms應用程序。 單擊它會打開一個表單。 這很好。

    notifyIcon.Click += notifyIcon_Click;

//Fires on icon click, AND on contextmenuitem click
    private void notifyIcon_Click(object sender, EventArgs e)
            {
                new ActiveIssues(_hubProxy).Show();
            }

我已經添加了一個上下文菜單,但是當我單擊ContextMenuItem時,它將首先觸發NotifyIcon click事件,然后觸發ContextMenuItem click事件,同時打開兩個窗體。

    notifyIcon.ContextMenu = GetCrestContextMenu();

     private ContextMenu GetCrestContextMenu()
            {
                var contextMenu = new ContextMenu();
                contextMenu.Name = "CResT Alerts";
                contextMenu.MenuItems.Add(GetTextOptionMenuItem());
                return contextMenu;
            }

            private MenuItem GetTextOptionMenuItem()
            {
                var textOptionMenuItem = new MenuItem { Text = _textOptedIn ? "Opt Out of Text Alerts" : "Opt In to Text Alerts" };
                textOptionMenuItem.Click += TextOptionMenuItem_Click;
                return textOptionMenuItem;
            }

//Fires on menuitem click, after the NotifyIcon click event is called
            private void TextOptionMenuItem_Click(object sender, EventArgs e)
            {
                if (_textOptedIn) new TextOptOut().Show();
                else new TextOptIn().Show();
            }

知道如何不讓它觸發notifiyicon單擊事件或告訴您單擊是在上下文菜單上嗎?

因此,事實證明,直到單擊上下文菜單之后,才注冊右鍵單擊,因此是單擊右鍵注冊並引發了NotifyIcon單擊事件。 因此,我必須將為單擊提供的EventArgs轉換為MouseEventArgs,然后檢查按鈕。

private void notifyIcon_Click(object sender, EventArgs e)
    {
        if(((MouseEventArgs)e).Button == MouseButtons.Left) new ActiveIssues(_hubProxy).Show();
    }

暫無
暫無

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

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