调用的NotifyIcon的上下文菜单 [英] Invoke NotifyIcon's Context Menu

查看:165
本文介绍了调用的NotifyIcon的上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有它,这样留给点击的NotifyIcon也使上下文菜单(与ContextMenuStrip属性设置),打开为好。我将如何实现这一目标?我一定要处理点击并找出自己的定位?结果
编辑:显示与trayIcon.ContextMenuStrip.Show菜单()的结果是一些不良行为:

I want to have it such that left clicking on the NotifyIcon also causes the context menu (set with the ContextMenuStrip property) to open as well. How would I achieve this? Do I have to handle Click and figure out the positioning myself?
showing the menu with trayIcon.ContextMenuStrip.Show() results is a few undesirable behaviors:

菜单是不是在同一位置显示,如果右键单击NotifyIcon的(看来,你不能设置x和y COORDS到任务栏,至少在Windows 7这是我跑什么)。它会出现在任务栏上(而不是什么大不了的事,但一致性将是很好)。

The menu is not shown at the same location as if right click the NotifyIcon (it appears that you can't set the x and y coords to where the taskbar is, at least on Windows 7 which is what I'm running). It will appear above the task bar (not that big of a deal, but consistency would be nice).

而在显示菜单里,有一个额外的图标添加到任务栏。

While the menu is shown, there is an extra icon added to the task bar.

点击比其他菜单某处不关闭它(而如果你右击,弹出右键菜单点击其他地方将自动关闭上下文菜单)。

Clicking somewhere other than the menu does not close it (whereas if you right click to bring up the context menu clicking else where automatically closes the context menu).

是它在所有可能只是调用菜单但是内置的右键处理程序是干嘛的?

Is it at all possible to just invoke the menu however the built in right click handler is doing it?

推荐答案

您通常会处理MouseClick事件检测点击调用ContextMenuStrip.Show()方法:

You would normally handle the MouseClick event to detect the click and call the ContextMenuStrip.Show() method:

    private void notifyIcon1_MouseClick(object sender, MouseEventArgs e) {
        contextMenuStrip1.Show(Control.MousePosition);
    }

但实际上并没有正常工作,当你点击它外面CMS不会关闭。根本的问题是Windows怪癖(又名错误),它在描述此知识库文章

在自己的code调用此解决方法是pretty痛苦的PInvoke的是不愉快的。 NotifyIcon的类有此解决办法在其<一href=\"http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/NotifyIcon.cs,fc0e2a272ada0b8f\">ShowContextMenu()方法,他们只是做它去,因为它是一个私有方法困难。反射可以绕过限制。我5年前发现了这个技巧,没有人报出了问题呢。设置NFI的ContextMenuStrip属性和实施这样的MouseUp事件:

Invoking this workaround in your own code is pretty painful, the pinvoke is unpleasant. The NotifyIcon class has this workaround in its ShowContextMenu() method, they just made it difficult to get to since it is a private method. Reflection can bypass that restriction. I discovered this hack 5 years ago and nobody reported a problem with it yet. Set the NFI's ContextMenuStrip property and implement the MouseUp event like this:

using System.Reflection;
...
    private void notifyIcon1_MouseUp(object sender, MouseEventArgs e) {
      if (e.Button == MouseButtons.Left) {
        MethodInfo mi = typeof(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic);
        mi.Invoke(notifyIcon1, null);
      }
    }

这篇关于调用的NotifyIcon的上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆