Determining location of tray icon
Asked Answered
C

2

9

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.

Cadre answered 6/12, 2010 at 12:37 Comment(0)
J
8

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.

Janinejanis answered 6/12, 2010 at 12:46 Comment(2)
Thank you very much :) I'll see if I can get it working with NotifyIcon and report back.Cadre
I've got the Shell_NotifyIconGetRect() function working with the WinForms NotifyIcon class. I documented the process in a blog post here: blog.quppa.net/?p=17Cadre
C
5

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/

Cadre answered 27/12, 2010 at 14:53 Comment(2)
Thanks. Does this one work only on older Windowses? I mean does it work on newer versions (Win 7,8)?Longplaying
I'd highly recommend using the Shell_NotifyIconGetRect function in Windows 7 and above, but the method I've described here should still work in newer versions of Windows. It won't find the location within the pop-up box, however.Cadre

© 2022 - 2024 — McMap. All rights reserved.