如果您按住 Ctrl 键单击或按住 Rt 键单击通知图标,如何获得不同的上下文菜单?

发布于 2024-09-24 12:30:36 字数 858 浏览 0 评论 0原文

我有一个基于系统托盘的应用程序。如果您右键单击它,我有一个很好的上下文菜单,但是如果您左键单击它,我希望显示一个不同的上下文菜单。现在,我使不同的菜单显示出来,

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

}

这使菜单显示出来,但单击菜单不会使其消失,使菜单消失的唯一方法是单击某个项目或单击托盘图标。

我也想出了这个技巧,但它确实感觉这是正确的方法。

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;
    }
}

这是正确的方法还是有更优雅的方法?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2024-10-01 12:30:36

由于没有其他人发布了一种有效的方法,我想正确的方法是

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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文