How to find the location of the icon in the system tray
Asked Answered
H

3

5

I have a NotifyIcon control that appears in the system tray. How can I find the location (x, y) of the icon on the screen?

Thanks

Hathaway answered 7/11, 2008 at 17:10 Comment(0)
F
1

You can't. It isn't hard to get the window handle for the system tray, GetClassName() returns "ToolbarWindow32", the class name of the standard TOOLBAR common control. Beware that a 64-bit OS has two of them. Then you can send messages to it like TB_GETBUTTONINFO. Chief hang-ups are that you won't know what button ID to choose and the returned info does not include the button position.

Which is for the better, buttons move around without you being able to lock them.

Fugal answered 7/11, 2008 at 20:25 Comment(2)
github.com/rzhw/SuperNotifyIcon clearly does this. I understand it may not have had exposure when this was answered - but it completes the task via native calls like you say is impossible - which works on older versions of windows. It also can do this via new Windows 7 API. A "I don't think you can" would've been a better answer than "You can't" - that's a bit of a reach.Fingering
Give the author credit for not ignoring the last sentence of my post. He did make it pretty clear by putting the code in the Deprecated folder and using comments like "Here be dragons". You can ignore the advice, you can downvote it, that does not make in conveniently invalid.Fugal
H
6

Actually you can do this, here is code that shows you how and much more.

In any Mouse Event from NotifyIcon, simply look at Control.MousePosition, this contains the (x,y) of the mouse. The same can be done to position a ContextMenu on a form/control exactly where the form/control was clicked using these (x,y) values.

A good example of both of these is here:

More Here: http://code.msdn.microsoft.com/TheNotifyIconExample

Herbal answered 7/11, 2008 at 17:10 Comment(0)
A
2

After a long journey of trial and error, and the development of my own notify icon locator, I developed a way to find the best position icon, so far so good, until I saw this amazing project by @zhwang SuperNotifyIcon. This project has three ways to locate the notification icon, ranging from the inaccurate (like my own development) to the use of new in Windows 7 API. This is the definitive answer.

Ameliaamelie answered 7/6, 2012 at 14:11 Comment(0)
F
1

You can't. It isn't hard to get the window handle for the system tray, GetClassName() returns "ToolbarWindow32", the class name of the standard TOOLBAR common control. Beware that a 64-bit OS has two of them. Then you can send messages to it like TB_GETBUTTONINFO. Chief hang-ups are that you won't know what button ID to choose and the returned info does not include the button position.

Which is for the better, buttons move around without you being able to lock them.

Fugal answered 7/11, 2008 at 20:25 Comment(2)
github.com/rzhw/SuperNotifyIcon clearly does this. I understand it may not have had exposure when this was answered - but it completes the task via native calls like you say is impossible - which works on older versions of windows. It also can do this via new Windows 7 API. A "I don't think you can" would've been a better answer than "You can't" - that's a bit of a reach.Fingering
Give the author credit for not ignoring the last sentence of my post. He did make it pretty clear by putting the code in the Deprecated folder and using comments like "Here be dragons". You can ignore the advice, you can downvote it, that does not make in conveniently invalid.Fugal

© 2022 - 2024 — McMap. All rights reserved.