Sco*_*ain 6 c# contextmenu notifyicon winforms
我有一个基于系统托盘的应用程序.如果你右键单击它我有一个很好的上下文菜单但是我想要一个不同的上下文菜单,如果你点击它就会出现.现在我把不同的菜单显示出来
private void niTrayIcon_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
cmsTrayLeftClick.Show(Cursor.Position);
}
}
Run Code Online (Sandbox Code Playgroud)
这使得菜单显示但单击菜单不会使其消失,使菜单消失的唯一方法是单击项目或rt单击托盘图标.
我也想出了这个黑客,但它确实感觉这是正确的方法.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是正确的做法还是有更优雅的方式?
因为没有其他人发布了一种有效的方法,我想正确的做法是
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1952 次 |
| 最近记录: |