AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Lib] TrayIcon_...()

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4633
Location: AHK Forum

PostPosted: Sun Jun 07, 2009 7:19 pm    Post subject: [Lib] TrayIcon_...() Reply with quote

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%
}

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink


Last edited by HotKeyIt on Fri Feb 26, 2010 10:31 pm; edited 6 times in total
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4633
Location: AHK Forum

PostPosted: Tue Jun 09, 2009 7:20 am    Post subject: Reply with quote

Fixed to get right position when Icon is hidden.
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
pajenn



Joined: 07 Feb 2009
Posts: 384

PostPosted: Tue Jun 09, 2009 8:32 am    Post subject: Reply with quote

Very useful function.

I've been working on a substitute for Windows 'Safely Remove Hardware' tray function. This may be just the ticket to nail down some pop-up menus involved.

Just one question or observation: When I ran this for one of my icons, I got the coordinates, x995 y16. Are those relative coordinates for the task/tray bar? Or is the y-coordinate measured from the bottom of the screen? Would it not be better to just return the coordinates in screen mode to ease their use with various mouse move, mouse click, etc. commands? Then again, I suppose the y-coordinate is always the same for all icons, so it doesn't really matter.
_________________
Hardware: 1.8 GHz laptop with 4 GB ram, Windows XP/SP3
Software: Prevx, Privatefirewall, KeyScrambler.
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4633
Location: AHK Forum

PostPosted: Tue Jun 09, 2009 9:10 am    Post subject: Reply with quote

pajenn wrote:
Very useful function.

I've been working on a substitute for Windows 'Safely Remove Hardware' tray function. This may be just the ticket to nail down some pop-up menus involved.

Just one question or observation: When I ran this for one of my icons, I got the coordinates, x995 y16. Are those relative coordinates for the task/tray bar? Or is the y-coordinate measured from the bottom of the screen? Would it not be better to just return the coordinates in screen mode to ease their use with various mouse move, mouse click, etc. commands? Then again, I suppose the y-coordinate is always the same for all icons, so it doesn't really matter.


Thanks pajenn.

The coordinates are relative to the window (Shell_TrayWnd).
To receive screen coordinates you will need only WinGetPos command and add values to x and y.
Especially for ControlClick you will need relative coordinates Exclamation
Code:
ControlClick % TrayIcon_GetPos(),ahk_class Shell_TrayWnd,,R ;right click on scripts icon

Y coordinates will be different only if you have several rows of icons!
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink


Last edited by HotKeyIt on Mon Jul 06, 2009 5:32 pm; edited 1 time in total
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4633
Location: AHK Forum

PostPosted: Fri Jun 19, 2009 6:56 pm    Post subject: Reply with quote

New function: TrayIcon_Hide(PID,hide=1), see top post.
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
Drugwash



Joined: 07 Sep 2008
Posts: 921
Location: Ploiesti, RO

PostPosted: Sat Jun 20, 2009 5:16 pm    Post subject: Reply with quote

HotKeyIt wrote:
Y coordinates will be different only if you have several rows of icons!
And also if the taskbar is positioned vertically, to the left or right. Wink

There are also themes that change tray icon size; it may be useful to get the size too, although this may only interest a handful of people.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
HotKeyIt



Joined: 18 Jun 2008
Posts: 4633
Location: AHK Forum

PostPosted: Sun Jun 21, 2009 9:35 am    Post subject: Reply with quote

Drugwash wrote:
HotKeyIt wrote:
Y coordinates will be different only if you have several rows of icons!
And also if the taskbar is positioned vertically, to the left or right. Wink

There are also themes that change tray icon size; it may be useful to get the size too, although this may only interest a handful of people.

TrayIcon_GetPos already considers the size of those Smile
_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
kenn



Joined: 11 Oct 2010
Posts: 279

PostPosted: Sat Dec 31, 2011 6:16 pm    Post subject: Reply with quote

Hi HotkeyIt, I'm trying to make it work on Win7 32 bit AHK_L but it returns the first icon's position in systray not the one I look for. What am I missing?
Edit: I run it on AHK Basic and it works.
Code:
F1::
detecthiddenwindows, on
CoordMode,Mouse,screen
process, exist, lingoes.exe
v := errorlevel
 
 TrayIcon_GetPosByRef(x,y,v)
WinGetPos,xc,yc,,,ahk_class Shell_TrayWnd
   x+=xc
   y+=yc
 
  ; MsgBox % "x: " Round(x) "`ny: " Round(y)
   MouseMove, %x%, %y%
 

return

#include remotebuf.ahk
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%
}
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group