c# - 如果您在通知图标上按住 Lt-Click 或 Rt-Click,如何获得不同的上下文菜单?

标签 c# winforms contextmenu notifyicon

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

显示不同的菜单
private void niTrayIcon_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Left)
    {
        cmsTrayLeftClick.Show(Cursor.Position);
    }

}

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

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

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

关于c# - 如果您在通知图标上按住 Lt-Click 或 Rt-Click,如何获得不同的上下文菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3780607/

相关文章:

c# - ASP :dropdownlist is null on page_load

c# - 对象引用方法调用

c# - WinForm 用户界面验证

.net - DataGridView 何时从自定义 EditingControl 获取其值?

c# - 单击鼠标左键打开上下文菜单 wpf c#

java - 上下文右键单击将链接另存为

android - 从 oncontextitemselected 获取 View ID

sql-server - 将 c# 模型类参数调用限制为不存在的表字段的 MS SQL 服务器

c# - 为什么 DateTime.Parse 这么慢?

c# - 如何使用 DbfDotNet 填充 Windows 窗体组合框?