NotifyIcon的文本菜单和太多的点击事件太多、菜单、文本、事件

2023-09-05 01:21:10 作者:曾经最爱的人

我使用了的NotifyIcon 类来显示在任务栏中的图标。图标执行2个功能 - 当用户单一的点击左键应该显示一个窗口,当用户单一的点击右边按钮,它应该显示上下文菜单。该用户点击上下文菜单中的选项后,显示工作正常,除了窗口。这是我的code:

I'm using the NotifyIcon class to display an icon in the task tray. The icon performs 2 functions - when the user single-clicks with the left button it should display a window, when the user single-clicks with the right button it should display the context menu. This works fine apart from the window being displayed after the user clicks an option in the context menu. Here's my code:

contextMenuItems = new List<MenuItem>();
contextMenuItems.Add(new MenuItem("Function A", new EventHandler(a_Clicked)));
contextMenuItems.Add(new MenuItem("-"));
contextMenuItems.Add(new MenuItem("Function B", new EventHandler(b_Clicked)));
trayIcon = new System.Windows.Forms.NotifyIcon();
trayIcon.MouseClick += new MouseEventHandler(trayIcon_IconClicked);
trayIcon.Icon = new Icon(GetType(), "Icon.ico");
trayIcon.ContextMenu = contextMenu;
trayIcon.Visible = true;

现在的问题是,当用户选择功能A或功能B我的 trayIcon_IconClicked 事件。为什么会是这样吗?

The problem is that my trayIcon_IconClicked event is fired when the user selects "Function A" or "Function B". Why would that be happening?

谢谢, Ĵ

推荐答案

通过指定上下文菜单中的NotifyIcon控件,它会自动捕获右键单击,然后打开分配的上下文菜单中有。如果你想在实际显示的上下文菜单中执行一些逻辑,然后分配代表对contextMenu.Popup事件。

By assigning the context menu to the NotifyIcon control, it automatically catches the right-click and opens the assigned context menu there. If you want to perform some logic before actually showing the context menu, then assign a delegate to the contextMenu.Popup event.

...
contextMenu.Popup += new EventHandler(contextMenu_Popup);
...

private void trayIcon_IconClicked(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        //Do something here.
    }
    /* Only do this if you're not setting the trayIcon.ContextMenu property, 
    otherwise use the contextMenu.Popup event.
    else if(e.Button == MouseButtons.Right)
    {
        //Show uses assigned controls Client location to set position, 
        //so must go from screen to client coords.
        contextMenu.Show(this, this.PointToClient(Cursor.Position));
    }
    */
}

private void contextMenu_Popup(object sender, EventArgs e)
{
    //Do something before showing the context menu.
}

我的猜测,为什么窗口弹出的是,你打开上下文菜单是使用的NotifyIcon作为目标的控制,所以,当你点击它它的运行,你分配到NotifyIcon的点击处理程序。

My guess as to why the window is popping up is that the context menu that you're opening up is using the NotifyIcon as the target control, so when you click on it it's running the click handler you assigned to the NotifyIcon.

编辑:的另一种选择要考虑的是使用的ContextMenuStrip来代替。 NotifyIcon类有一个ContextMenuStrip属性,以及,它似乎有一个与之相关的更多的功能(注意到我可以做更多的,可编程的智慧)。可能想给一个镜头,如果事情不工作的权利的某些原因。

Another option to consider is to use ContextMenuStrip instead. The NotifyIcon has a ContextMenuStrip property as well, and it seems to have a lot more functionality associated with it (noticed I could do more, programmable wise). Might want to give that a shot if things aren't working right for some reason.