如果您按住 Ctrl 键单击或按住 Rt 键单击通知图标,如何获得不同的上下文菜单?
我有一个基于系统托盘的应用程序。如果您右键单击它,我有一个很好的上下文菜单,但是如果您左键单击它,我希望显示一个不同的上下文菜单。现在,我使不同的菜单显示出来,
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于没有其他人发布了一种有效的方法,我想正确的方法是
As no one else has posted a way that works I guess the correct way of doing it is