确定任务栏图标的位置 [英] Determining location of tray icon

查看:156
本文介绍了确定任务栏图标的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序被设计为位于系统托盘中,其行为与Windows 7中的网络/卷/电源/操作中心"托盘项目(以及Windows Vista中的等效项目)相似.也就是说,单击任务栏图标时该图标变为可见,而丢失焦点时该图标变为隐藏.

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.

该应用程序是用WPF编写的,但是我目前正在使用WinForms中的NotifyIcon作为任务栏图标.

The application is written in WPF, but I am currently using NotifyIcon from WinForms for the tray icon.

我想知道是否有人对我如何确定应用程序托盘图标的位置(即屏幕坐标)有任何建议.默认的Windows Vista/7托盘项目可以执行某些操作,因为弹出窗口在相关托盘图标上方居中对齐.

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.

在Vista中,这不是什么大问题:我将应用程序永久设置在右下角,而且看起来还不错(尽管对于没有将任务栏放在底部的情况没有任何逻辑屏幕上的).但是,在Windows 7中,我的应用程序最终使新的系统托盘弹出框变得模糊,而不是像音量控件等那样悬停在其上方.

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.

到目前为止,我在搜索中看到的最佳解决方案是处理NotifyIcon上的mouse-up事件,并使用鼠标坐标来确定图标的位置.我认为如果除非有人有更好的主意,否则我将最终使用此方法,但这并不理想,因为位置会根据用户单击图标的位置而略有不同.

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.

推荐答案

Windows 7和更高版本公开了

Windows 7 and higher expose the Shell_NotifyIconGetRect() function, which returns the screen coordinates of the icon's bounding rectangle.

不过,您需要为其提供通知图标GUID,我不知道您是否可以通过托管的

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.

这篇关于确定任务栏图标的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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