C# 如果左键单击或右键单击通知图标,如何获得不同的上下文菜单?
我有一个基于系统托盘外的应用程序。我有一个很好的上下文菜单,如果你右键点击它,但我想一个不同的上下文菜单显示,如果你左键点击它。现在,我让不同的菜单显示C# 如果左键单击或右键单击通知图标,如何获得不同的上下文菜单?,c#,winforms,contextmenu,notifyicon,C#,Winforms,Contextmenu,Notifyicon,我有一个基于系统托盘外的应用程序。我有一个很好的上下文菜单,如果你右键点击它,但我想一个不同的上下文菜单显示,如果你左键点击它。现在,我让不同的菜单显示 private void niTrayIcon_MouseClick(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { cmsTrayLeftClick.Show(Cursor.
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;
}
}
这是正确的方法还是有更优雅的方法?因为没有其他人发布过有效的方法,我想正确的方法是
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;
}
}
您的解决方案非常有效,它还解决了我的一些其他问题:当我单击通知图标时,我的contextmenustrip远离鼠标指针。现在它完全对齐了。