确定托盘图标的位置
我的应用程序设计为位于系统托盘中,其行为类似于 Windows 7 中的网络/音量/电源/操作中心托盘项目(以及 Windows Vista 中的等效项目)。也就是说,当单击托盘图标时它变得可见,而当失去焦点时它变得隐藏。
该应用程序是用 WPF 编写的,但我目前使用 WinForms 中的 NotifyIcon 作为托盘图标。
我想知道是否有人对如何确定应用程序托盘图标的位置(即屏幕坐标)有任何建议。默认的 Windows Vista/7 托盘项有一些方法可以做到这一点,因为弹出窗口在相关托盘图标上方居中对齐。
在 Vista 中,这不是什么大问题:我将应用程序永久设置在右下角,这看起来不错(尽管没有包含任务栏未放置在屏幕底部的情况的逻辑) )。然而,在 Windows 7 中,我的应用程序最终会遮挡新的系统托盘弹出框,而不是像音量控件等那样悬停在其上方。
到目前为止,我在搜索中看到的最佳解决方案是处理 NotifyIcon 上的鼠标松开事件,并使用鼠标坐标来确定图标的位置。我想我最终会使用这种方法,除非有人有更好的想法,尽管它并不理想,因为位置会根据用户单击图标内的位置而略有不同。
My application is designed to sit in the system tray and behave similarly to the Network/Volume/Power/Action Centre tray items in Windows 7 (and the equivalent items in Windows Vista). That is, it becomes visible when the tray icon is clicked, and becomes hidden when focus is lost.
The application is written in WPF, but I am currently using NotifyIcon from WinForms for the tray icon.
I would like to know if anyone has any suggestions as to how I might determine the position (i.e. screen coordinates) of my application's tray icon. The default Windows Vista/7 tray items have some way of doing this, since the pop-out windows are centre-aligned above the relevant tray icon.
In Vista this wasn't much of an issue: I had the application permanently set to the bottom-right corner, and this looked fine (though there was no logic included for cases when the taskbar wasn't placed at the bottom of the screen). However, in Windows 7 my application ends up obscuring the new system tray pop-out box, rather than hovering above it like the volume controls, etc., do.
The best solution I've seen in my searches so far is to handle the mouse-up event on the NotifyIcon and use the mouse coordinates to determine the position of the icon. I think I'll end up using this method if unless someone has a better idea, though it's not ideal as the position will vary slightly depending on where within the icon the user clicks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Windows 7 及更高版本公开 Shell_NotifyIconGetRect() 函数,返回图标边界矩形的屏幕坐标。
不过,您需要为其提供通知图标 GUID,并且我不知道您是否可以通过托管 NotifyIcon 类。
Windows 7 and higher expose the Shell_NotifyIconGetRect() function, which returns the screen coordinates of the icon's bounding rectangle.
You'll need to provide it the notify icon GUID, though, and I don't know if you can access that property through the managed NotifyIcon class.
除此之外,我还写了一种在 Shell_NotifyIconGetRect 函数不可用时查找通知图标位置的方法:http://blog.quppa.net/2010/12 /28/windows-7-style-notification-area-applications-in-wpf-part-6-notify-icon-position-pre-windows-7/
Further to this, I've written about a method to find the location of a notify icon when the Shell_NotifyIconGetRect function is not available: http://blog.quppa.net/2010/12/28/windows-7-style-notification-area-applications-in-wpf-part-6-notify-icon-position-pre-windows-7/