Using this functions you can receive coordinates for a TrayIcon of any application.
Now you can hide and show an Icon as well.
To use this function you will need
[module] RemoteBuffer 2.0 (RemoteBuf.ahk) in same folder as this script or in your Lib folder.
Many thanks to
- Irek Zielinski for
CTrayIconPosition - where is my tray icon?
- majkinetor for RemoteBuffer
Code:
/*
TrayIcon_GetPos
TrayIcon_GetPosByRef
Use: Using this function you can receive coordinates for a TrayIcon of any application
Syntax: GetTrayIconPos([PID]) - To get a PID use Process, Exist, application.exe, if ommited, PID of this script is used
Returns string "xn yn", n=coordinates in Pixel
SyntaxByRef:TrayIcon_GetPosByRef(x,y[,PID]) - x and y will be variables that will hold coordinates after function is called
TrayIcon_Hide
Use: Using this function you can hide and show a tray icon giving its PID
Syntax: TrayIcon_Hide(PID,1=hide or 0=show)
*/
MsgBox % TrayIcon_GetPos(4004) ;Returns coordinates string
MsgBox % "Hidden " TrayIcon_Hide("",1)
MsgBox % "Hidden " TrayIcon_Hide("",0)
;or
TrayIcon_GetPosByRef(x,y) ;save x and y coordinates in x and y variable
MsgBox % "x: " Round(x) "`ny: " Round(y)
ControlClick,% TrayIcon_GetPos(),ahk_class Shell_TrayWnd,,R
Sleep, 3000
;or
CoordMode,Mouse,screen
TrayIcon_GetPosByRef(x,y) ;save x and y coordinates in x and y variable
WinGetPos,xc,yc,,,ahk_class Shell_TrayWnd
x+=xc
y+=yc
Click R %x% %y%
MsgBox % "x: " Round(x) "`ny: " Round(y)
ExitApp
TrayIcon_Hide(PID="",hide=1){
If ((#_DetectHiddenWindows:=A_DetectHiddenWindows)="Off")
DetectHiddenWindows, On
If !PID
{
Process, Exist
PID:=ErrorLevel
}
hWndTray:=WinExist("ahk_class Shell_TrayWnd")
ControlGet,hWndToolBar,Hwnd,,ToolbarWindow321,ahk_id %hWndTray%
RemoteBuf_Open(TrayH,hWndToolBar,20)
DataH:=NumGet(TrayH,0)
SendMessage, 0x418,0,0,,ahk_id %hWndToolBar%
ErrorLvl:=ErrorLevel
Loop % ErrorLevel
{
SendMessage,0x417,A_Index-1,RemoteBuf_Get(TrayH),,ahk_id %hWndToolBar%
RemoteBuf_Read(TrayH,lpData,20)
pwData:=NumGet(lpData,12)
VarSetCapacity(dwExtraData,20)
DllCall( "ReadProcessMemory", "uint", DataH, "uint", pwData, "uint", &dwExtraData, "uint", 8, "uint", 0 )
BWID:=NumGet(dwExtraData,0)
WinGet,BWPID,PID, ahk_id %BWID%
If (BWPID!=PID)
continue
SendMessage, 0x404,ErrorLvl - A_Index,hide,,ahk_id %hWndToolBar%
break
}
RemoteBuf_close(TrayH)
DetectHiddenWindows, %#_DetectHiddenWindows%
Return hide
}
TrayIcon_GetPos(PID=""){
If ((#_DetectHiddenWindows:=A_DetectHiddenWindows)="Off")
DetectHiddenWindows, On
If !PID
{
Process, Exist
PID:=ErrorLevel
}
hWndTray:=WinExist("ahk_class Shell_TrayWnd")
ControlGet,hWndToolBar,Hwnd,,ToolbarWindow321,ahk_id %hWndTray%
RemoteBuf_Open(TrayH,hWndToolBar,20)
DataH:=NumGet(TrayH,0)
SendMessage, 0x418,0,0,,ahk_id %hWndToolBar%
Loop % ErrorLevel
{
SendMessage,0x417,A_Index-1,RemoteBuf_Get(TrayH),,ahk_id %hWndToolBar%
RemoteBuf_Read(TrayH,lpData,20)
pwData:=NumGet(lpData,12)
VarSetCapacity(dwExtraData,20)
DllCall( "ReadProcessMemory", "uint", DataH, "uint", pwData, "uint", &dwExtraData, "uint", 8, "uint", 0 )
BWID:=NumGet(dwExtraData,0)
WinGet,BWPID,PID, ahk_id %BWID%
If (BWPID!=PID)
continue
SendMessage, 0x41d,A_Index-1,RemoteBuf_Get(TrayH),,ahk_id %hWndToolBar%
RemoteBuf_Read(TrayH,rcPosition,20)
If (NumGet(lpData,8)>7){
ControlGetPos,x,y,w,h,Button2,ahk_id %hWndTray%
x+=w/2
y+=h/4
} else {
ControlGetPos,x,y,,,ToolbarWindow321,ahk_id %hWndTray%
halfsize:=NumGet(rcPosition,12)/2
x+=NumGet(rcPosition,0)+ halfsize
y+=NumGet(rcPosition,4)+ halfsize
}
}
RemoteBuf_close(TrayH)
DetectHiddenWindows, %#_DetectHiddenWindows%
Return "x" . Round(x) . " y" . Round(y)
}
TrayIcon_GetPosByRef(ByRef x, ByRef y,PID=""){
If ((#_DetectHiddenWindows:=A_DetectHiddenWindows)="Off")
DetectHiddenWindows, On
If !PID
{
Process, Exist
PID:=ErrorLevel
}
hWndTray:=WinExist("ahk_class Shell_TrayWnd")
ControlGet,hWndToolBar,Hwnd,,ToolbarWindow321,ahk_id %hWndTray%
RemoteBuf_Open(TrayH,hWndToolBar,20)
DataH:=NumGet(TrayH,0)
SendMessage, 0x418,0,0,,ahk_id %hWndToolBar%
Loop % ErrorLevel
{
SendMessage,0x417,A_Index-1,RemoteBuf_Get(TrayH),,ahk_id %hWndToolBar%
RemoteBuf_Read(TrayH,lpData,20)
pwData:=NumGet(lpData,12)
VarSetCapacity(dwExtraData,8)
DllCall( "ReadProcessMemory", "uint", DataH, "uint", pwData, "uint", &dwExtraData, "uint", 8, "uint", 0 )
BWID:=NumGet(dwExtraData,0)
WinGet,BWPID,PID, ahk_id %BWID%
If (BWPID!=PID)
continue
SendMessage, 0x41d,A_Index-1,RemoteBuf_Get(TrayH),,ahk_id %hWndToolBar%
RemoteBuf_Read(TrayH,rcPosition,20)
If (NumGet(lpData,8)>7){
ControlGetPos,x,y,w,h,Button2,ahk_id %hWndTray%
x+=w/2
y+=h/2
} else {
ControlGetPos,x,y,,,ToolbarWindow321,ahk_id %hWndTray%
halfsize:=NumGet(rcPosition,12)/2
x+=NumGet(rcPosition,0)+ halfsize
y+=NumGet(rcPosition,4)+ halfsize
}
}
RemoteBuf_close(TrayH)
DetectHiddenWindows, %#_DetectHiddenWindows%
}