显示 NotifyIcon 上下文菜单并控制其位置?

发布于 2024-08-25 14:44:40 字数 736 浏览 3 评论 0原文

我试图在左键单击 NotifyIcon 时显示上下文菜单。仅调用 NotifyIcon.ContextMenuStrip.Show() 效果不太好。 之前已在此发布了一个解决方案,该解决方案使用反射调用秘密方法:

Dim mi As System.Reflection.MethodInfo = GetType(NotifyIcon).GetMethod("ShowContextMenu", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
mi.Invoke(Icon, Nothing)

这非常有效,除了我还需要控制菜单的显示位置。我想等待 SystemInformation.DoubleClickTime 在接收 NotifyIcon.MouseUp 事件和显示菜单之间过去,以便我可以分别处理单击和双击。但是,调用 ShowContextMenu 方法会在调用 ShowContextMenu 时(而不是实际单击图标时)在当前鼠标位置显示菜单。这意味着如果鼠标在 DoubleClickTime 期间移动,菜单将显示在屏幕的不同部分。因此,如果我可以控制菜单的显示位置,那么我只需在收到 MouseUp 事件时保存鼠标坐标,然后就可以确保菜单显示在图标附近。有办法做到这一点吗?

提前致谢。

I'm trying to show a context menu when I left-click a NotifyIcon. Just calling NotifyIcon.ContextMenuStrip.Show() doesn't work very well. A solution has been posted here before that calls a secret method using Reflection:

Dim mi As System.Reflection.MethodInfo = GetType(NotifyIcon).GetMethod("ShowContextMenu", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
mi.Invoke(Icon, Nothing)

This works great, except that I also need to control where the menu is shown. I want to wait for the SystemInformation.DoubleClickTime to elapse between receiving the NotifyIcon.MouseUp event and displaying the menu, so that I can handle single-clicks and double-clicks separately. But invoking the ShowContextMenu method displays the menu at the current mouse position when ShowContextMenu is called, not when the icon was actually clicked. Which means that if the mouse moved during the DoubleClickTime, the menu will be displayed in a different part of the screen. So if I can control where the menu is shown, I can just save the mouse coordinates when I receive the MouseUp event, and then I can ensure that the menu is displayed near the icon. Is there a way to do this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

横笛休吹塞上声 2024-09-01 14:44:40

嗯,我刚刚发现现有的程序也表现出同样的行为。我刚刚浏览了系统托盘中的所有图标,其中大约有一半是这样的。如果您左键单击该图标,然后在菜单出现之前的延迟期间移动鼠标,则菜单将出现在鼠标最后的位置,无论该位置在屏幕上的任何位置。 Snagit 是执行此操作的一个应用程序。 Outlook 是我的托盘中唯一始终显示我单击图标的菜单的程序。但 Snagit 看起来使用的是 .NET ContextMenuStrip,而 Outlook 可能使用的是本机菜单。

因此,这要么是标准行为,要么是其他人无法解决的问题。作为用户,直到昨天我测试自己的应用程序时,我才注意到这种行为。所以我想这没什么大不了的,我不会担心。

Well, I just discovered that there are existing programs that exhibit this same behavior. I just went through all the icons in my system tray and about half of them do it. If you left-click the icon and then move the mouse during the delay before the menu appears, the menu will appear at the last mouse location, wherever that is on the screen. Snagit is one application that does this. Outlook is the only program in my tray that always shows the menu where I clicked the icon. But Snagit looks like it's using a .NET ContextMenuStrip, while Outlook is probably using a native menu.

So either this is standard behavior, or it's a problem that no one else has been able to solve either. And as a user, I've never noticed this behavior until yesterday when I was testing my own application. So I guess it's not that big of a deal and I won't worry about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文