AutoHotkey Community

It is currently May 26th, 2012, 6:54 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: [Lib] TrayIcon_...()
PostPosted: June 7th, 2009, 8:19 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
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 February 26th, 2010, 11:31 pm, edited 6 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 9th, 2009, 8:20 am 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
Fixed to get right position when Icon is hidden.

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 9th, 2009, 9:32 am 
Offline

Joined: February 7th, 2009, 11:28 pm
Posts: 384
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 9th, 2009, 10:10 am 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
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 :!:
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 July 6th, 2009, 6:32 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 19th, 2009, 7:56 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
New function: TrayIcon_Hide(PID,hide=1), see top post.

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 20th, 2009, 6:16 pm 
Offline
User avatar

Joined: September 8th, 2008, 12:26 am
Posts: 1048
Location: Ploieşti, RO
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. ;)

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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 21st, 2009, 10:35 am 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
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. ;)

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

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 31st, 2011, 7:16 pm 
Offline

Joined: October 11th, 2010, 12:30 pm
Posts: 406
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%
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: fusion1920, infogulch and 11 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group