简体   繁体   English

如果您点击或点击通知图标,如何获得不同的上下文菜单?

[英]How do you get a different Context Menu if you Lt-Click or Rt-Click on a notify icon?

I have a application that is based out of the system tray. 我有一个基于系统托盘的应用程序。 I have a nice context menu if you right click on it however I would like a different context menu to show up if you left click on it. 如果你右键单击它我有一个很好的上下文菜单但是我想要一个不同的上下文菜单,如果你点击它就会出现。 Right now I make the different menu show up with 现在我把不同的菜单显示出来

private void niTrayIcon_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        cmsTrayLeftClick.Show(Cursor.Position);
    }

}

That makes the menu show up but clicking off the menu does not make it go away, the only way to make the menu disappear is either click on a item or rt click on the tray icon. 这使得菜单显示但单击菜单不会使其消失,使菜单消失的唯一方法是单击项目或rt单击托盘图标。

I have also came up with this hack but it does feel like it is the correct way to do it. 我也想出了这个黑客,但它确实感觉这是正确的方法。

private void niTrayIcon_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        niTrayIcon.ContextMenuStrip = cmsTrayLeftClick;
        MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
        mi.Invoke(niTrayIcon, null);
        niTrayIcon.ContextMenuStrip = cmsTrayRtClick;
    }
}

Is this the correct way of doing it or is there a more elegant way? 这是正确的做法还是有更优雅的方式?

As no one else has posted a way that works I guess the correct way of doing it is 因为没有其他人发布了一种有效的方法,我想正确的做法是

private void niTrayIcon_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        niTrayIcon.ContextMenuStrip = cmsTrayLeftClick;
        MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
        mi.Invoke(niTrayIcon, null);
        niTrayIcon.ContextMenuStrip = cmsTrayRtClick;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM