Getting the coordinates of the tray icon? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
william_ahk
Posts: 493
Joined: 03 Dec 2018, 20:02

Getting the coordinates of the tray icon?

Post by william_ahk » 27 May 2023, 11:03

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.

iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: Getting the coordinates of the tray icon?  Topic is solved

Post by iPhilip » 28 May 2023, 02:44

@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)

william_ahk
Posts: 493
Joined: 03 Dec 2018, 20:02

Re: Getting the coordinates of the tray icon?

Post by william_ahk » 28 May 2023, 06:10

@iPhilip It says unspecified error at this line if !DllCall('Shell32\Shell_NotifyIconGetRect', 'Ptr', NOTIFYICONIDENTIFIER, 'Ptr', RECT := Buffer(16), 'HRESULT')

iPhilip
Posts: 822
Joined: 02 Oct 2013, 12:21

Re: Getting the coordinates of the tray icon?

Post by iPhilip » 28 May 2023, 11:27

@william_ahk, Thank you for reporting the error. I fixed the issue by initializing the NOTIFYICONIDENTIFIER buffer to zero so that the requirement
is always met. It should work more reliably now.

Cheers!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

william_ahk
Posts: 493
Joined: 03 Dec 2018, 20:02

Re: Getting the coordinates of the tray icon?

Post by william_ahk » 29 May 2023, 03:14

@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")}
}

Post Reply

Return to “Ask for Help (v2)”