繁体   English   中英

与NotifyIcon关联时检查ContextMenu的可见性

[英]Checking the visibility of a ContextMenu when associated with a NotifyIcon

根据另一个问题的答案 ,只有在调用Show()之前将ContextMenu的Collapsed事件与控件关联,才会引发它。

因为NotifyIcon不算作控件,所以我无法挂起Collapsed事件来检测与之关联的菜单何时被隐藏。

有什么解决方法吗?

TrackPopupMenuEx上的MSDN文档的“备注”部分下,它说:

若要显示通知图标的上下文菜单,在应用程序调用TrackPopupMenu或TrackPopupMenuEx之前,当前窗口必须是前景窗口。 否则,当用户在菜单或创建菜单的窗口之外单击时(如果可见),菜单将不会消失。 如果当前窗口是子窗口,则必须将(顶层)父窗口设置为前景窗口。

因此,这意味着当ContextMenu可见时, NotifyIcon上的窗口将成为前景窗口。 通过查看NotifyIcon.ShowContextMenu()可以看到确实如此:

    private void ShowContextMenu()
    {
        if (this.contextMenu != null || this.contextMenuStrip != null)
        {
            NativeMethods.POINT pOINT = new NativeMethods.POINT();
            UnsafeNativeMethods.GetCursorPos(pOINT);
            UnsafeNativeMethods.SetForegroundWindow(new HandleRef(this.window, this.window.Handle));
            if (this.contextMenu != null)
            {
                this.contextMenu.OnPopup(EventArgs.Empty);
                SafeNativeMethods.TrackPopupMenuEx(new HandleRef(this.contextMenu, this.contextMenu.Handle), 72, pOINT.x, pOINT.y, new HandleRef(this.window, this.window.Handle), null);
                UnsafeNativeMethods.PostMessage(new HandleRef(this.window, this.window.Handle), 0, IntPtr.Zero, IntPtr.Zero);
                return;
            }
            if (this.contextMenuStrip != null)
            {
                this.contextMenuStrip.ShowInTaskbar(pOINT.x, pOINT.y);
            }
        }
    }

然后,我使用ILSpy注意到NotifyIcon有一个私有成员window ,该window引用具有基本类型NativeWindow的私有类。 因此,您可以像这样检查:

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetForegroundWindow();

...

FieldInfo notifyIconNativeWindowInfo = typeof(NotifyIcon).GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
NativeWindow notifyIconNativeWindow = (NativeWindow)notifyIconNativeWindowInfo.GetValue(notifyIcon1);

bool visible = notifyIcon1.Handle == GetForegroundWindow();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM