Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何将ContextMenuStrip附加到NotifyIcon_.net_Vb.net_Notifyicon_Contextmenustrip - Fatal编程技术网

.net 如何将ContextMenuStrip附加到NotifyIcon

.net 如何将ContextMenuStrip附加到NotifyIcon,.net,vb.net,notifyicon,contextmenustrip,.net,Vb.net,Notifyicon,Contextmenustrip,我正在尝试使NotifyIcon显示上下文菜单,即使用鼠标左键单击。我可以在图标的MouseDown事件中将其显示在正确的位置: sysTrayIcon.ContextMenuStrip = TrayContextMenu If e.Button = MouseButtons.Left Then TrayContextMenu.Show() 但由于左键单击时未将sysTrayIcon指定为控件,因此,如果在菜单外单击或按escape键,屏幕上不会清除该控件 我知道通常的方法是使用菜单的重载Sh

我正在尝试使
NotifyIcon
显示上下文菜单,即使用鼠标左键单击。我可以在图标的
MouseDown
事件中将其显示在正确的位置:

sysTrayIcon.ContextMenuStrip = TrayContextMenu
If e.Button = MouseButtons.Left Then TrayContextMenu.Show()
但由于左键单击时未将
sysTrayIcon
指定为控件,因此,如果在菜单外单击或按escape键,屏幕上不会清除该控件

我知道通常的方法是使用菜单的重载
Show(control,location)
方法,但这会导致此错误:

Value of type 'System.Windows.Forms.NotifyIcon' cannot be converted to 'System.Windows.Forms.Control'.

那么,我如何才能将菜单附加到通知图标上呢?

是的,此代码无法正常工作。要在正确的位置获取上下文菜单,并正确设置鼠标捕获,以便在其外部单击可以正常工作,需要几个选项。这些咒语是必需的,因为管理通知图标的是Windows资源管理器,而不是您的程序

您需要让NotifyIcon类来处理这个问题。然而,一个重要的障碍是它没有公开显示上下文菜单的方法,它是一个私有方法。对此,您唯一能做的就是使用反射来调用该方法。如下所示(使用默认名称):

Imports System.Reflection
...
    Private Sub NotifyIcon1_MouseDown(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDown
        NotifyIcon1.ContextMenuStrip = ContextMenuStrip1
        Dim mi = GetType(NotifyIcon).GetMethod("ShowContextMenu", BindingFlags.NonPublic Or BindingFlags.Instance)
        mi.Invoke(NotifyIcon1, Nothing)
    End Sub