Getting the coordinates of the tray icon? Topic is solved
-
- Posts: 493
- Joined: 03 Dec 2018, 20:02
Getting the coordinates of the tray icon?
I found TrayIcon_GetPos but it no longer seems to work as it crashes the Explorer. The newest TrayIcon lib doesn't seem to have this function.
Re: Getting the coordinates of the tray icon? Topic is solved
@william_ahk, would this work for you?
Code: Select all
#Requires AutoHotkey v2.0
RECT := GetTrayIconRECT(A_ScriptHwnd)
MsgBox 'Notification icon`nscreen coordinates:`n`nLeft:`t' RECT.Left '`nTop:`t' RECT.Top '`nRight:`t' RECT.Right '`nBottom:`t' RECT.Bottom
GetTrayIconRECT(hwnd) {
static AHK_NOTIFYICON := 0x0404
static Size := 16 + 3 * A_PtrSize
NOTIFYICONIDENTIFIER := Buffer(Size, 0)
NumPut 'UInt', Size, NOTIFYICONIDENTIFIER, 0
NumPut 'Ptr', hwnd, NOTIFYICONIDENTIFIER, A_PtrSize
NumPut 'UInt', AHK_NOTIFYICON, NOTIFYICONIDENTIFIER, 2 * A_PtrSize
if !DllCall('Shell32\Shell_NotifyIconGetRect', 'Ptr', NOTIFYICONIDENTIFIER, 'Ptr', RECT := Buffer(16), 'HRESULT')
return {Left: NumGet(RECT, 0, 'Int'), Top: NumGet(RECT, 4, 'Int'), Right: NumGet(RECT, 8, 'Int'), Bottom: NumGet(RECT, 12, 'Int')}
}
Last edited by iPhilip on 28 May 2023, 11:21, edited 1 time in total.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
-
- Posts: 493
- Joined: 03 Dec 2018, 20:02
Re: Getting the coordinates of the tray icon?
@iPhilip It says unspecified error at this line if !DllCall('Shell32\Shell_NotifyIconGetRect', 'Ptr', NOTIFYICONIDENTIFIER, 'Ptr', RECT := Buffer(16), 'HRESULT')
Re: Getting the coordinates of the tray icon?
@william_ahk, Thank you for reporting the error. I fixed the issue by initializing the NOTIFYICONIDENTIFIER buffer to zero so that the requirement
Cheers!
is always met. It should work more reliably now.https://learn.microsoft.com/en-us/windows/win32/api/shellapi/ns-shellapi-notifyiconidentifier#members wrote: Use GUID_NULL if the icon is not identified by a GUID.
Cheers!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
-
- Posts: 493
- Joined: 03 Dec 2018, 20:02
Re: Getting the coordinates of the tray icon?
@iPhilip Thank you so much! Works great. I incorporated a TrayIcon version:
Code: Select all
TrayIcon_GetPos(hwnd, uID) {
static Size := 16 + 3 * A_PtrSize
NID := Buffer(Size, 0)
NumPut("UInt", Size, NID, 0)
NumPut("Ptr", hWnd, NID, A_PtrSize)
NumPut("UInt", uID, NID, A_PtrSize * 2)
if !DllCall("Shell32\Shell_NotifyIconGetRect", "Ptr", NID, "Ptr", RECT := Buffer(16), "HRESULT")
return {Left: NumGet(RECT, 0, "Int"), Top: NumGet(RECT, 4, "Int"), Right: NumGet(RECT, 8, "Int"), Bottom: NumGet(RECT, 12, "Int")}
}