life ideas

October 23, 2006

trayicon interactive of AutoHotKey

Filed under: Uncategorized — manoftoday @ 10:31 pm

1) search through icon

 

Sleep, 100
hw_notification := FindWindow( “Shell_TrayWnd|TrayNotifyWnd|SysPager|ToolbarWindow32,Notification Area” )
WinGetPos, s_x, s_y, s_w, s_h, ahk_id %hw_notification%
CoordMode, Pixel, Screen
ImageSearch, t_x, t_y, s_x, s_y, s_x+s_w-1, s_y+s_h-1, *Icon1 %A_ProgramFiles%\autohotkey\autohotkey.exe
if ( ErrorLevel = 0 )
{
   CoordMode, Mouse, Screen
   MouseClick, Right, t_x, t_y, 1
}
return
FindWindow( p_tree )
{
   level_total = 0
   loop, parse, p_tree, |
   {
      level_total++
      ix := InStr( a_LoopField, “,” )
      if ( ix )
      {
         StringMid, tree[%level_total%]?class, a_LoopField, 1, ix-1
         StringMid, tree[%level_total%]?title, a_LoopField, ix+1, StrLen( a_LoopField )-ix
      }
      else
      {
         tree[%level_total%]?class := a_LoopField
         tree[%level_total%]?title = 0
      }
   }
   hw_parent = 0
   hw_child = 0
   level = 1
   loop,
   {
      hw_child := FindWindowEx( hw_parent, hw_child, tree[%level%]?class, tree[%level%]?title )
      if ( hw_child )
      {
         if ( level = level_total )
            return, hw_child
         level++
         hw_parent_old := hw_parent
         hw_parent := hw_child
         hw_child_old := hw_child   
         hw_child = 0
      }
      else
      {
         if ( level = 1 )
            return, 0
         level–
         hw_parent := hw_parent_old
         hw_child := hw_child_old
      }
   }
}
FindWindowEx( p_hw_parent, p_hw_child, p_class, p_title=0 )
{
   if ( p_title = 0 )
      type_title = uint
   else
      type_title = str
   return, DllCall( “FindWindowEx”
                  , “uint”, p_hw_parent
                  , “uint”, p_hw_child
                  , “str”, p_class
                  , type_title, p_title )
}

 

 

2)  walk through ICONS in tray

 

WM_USER = 0x400
hw_notification_area := FindWindow( “Shell_TrayWnd|TrayNotifyWnd|SysPager|ToolbarWindow32,Notification Area” )
MsgBox, hw_notification_area = %hw_notification_area%
VarSetCapacity( pid_explorer, 4 )
WinGet, pid_explorer, PID, ahk_id %hw_notification_area%
MsgBox, pid_explorer: %pid_explorer%
hp_explorer := DllCall( “OpenProcess”, “uint”, 0x8|0x10, “int”, true, “uint”, pid_explorer )
MsgBox, openprocess: %ErrorLevel% – %hp_explorer%
remote_buffer := DllCall( “VirtualAllocEx”
                     , “uint”, hp_explorer
                     , “uint”, 0
                     , “uint”, 0x1000
                     , “uint”, 0x1000
                     , “uint”, 0x4 )
MsgBox, virtualallocex: %ErrorLevel% – %remote_buffer%
TB_BUTTONCOUNT := WM_USER+24
SendMessage, TB_BUTTONCOUNT, 0, 0,, ahk_id %hw_notification_area%
MsgBox, button count = %ErrorLevel%
TB_GETBUTTON := WM_USER+23
SendMessage, TB_GETBUTTON, 0, remote_buffer,, ahk_id %hw_notification_area%
MsgBox, TB_GETBUTTON: %ErrorLevel%
remote_buffer2 := remote_buffer+200
TB_GETITEMRECT := WM_USER+29
SendMessage, TB_GETITEMRECT, index, remote_buffer2,, ahk_id %hw_notification_area%
MsgBox, TB_GETITEMRECT: %ErrorLevel%
VarSetCapacity( buffer, 1000, 0 )
VarSetCapacity( buffer_actual_size, 4, 0 )
pBuffer2 := &buffer+200
result := DllCall( “ReadProcessMemory”
                  , “uint”, hp_explorer
                  , “uint”, remote_buffer
                  , “uint”, &buffer
                  , “uint”, 1000
                  , “uint”, &buffer_actual_size )
buffer_actual_size := ReadInteger( “uint”, &buffer_actual_size, 0 )
MsgBox, readprocessmemory: %ErrorLevel% – %result% ~ %buffer_actual_size%
; — data —
VarSetCapacity( buffer2, 1000, 0 )
VarSetCapacity( buffer_actual_size, 4, 0 )
result := DllCall( “ReadProcessMemory”
                  , “uint”, hp_explorer
                  , “uint”, ReadInteger( “int”, &buffer, 12 )
                  , “uint”, &buffer2
                  , “uint”, 1000
                  , “uint”, &buffer_actual_size )
buffer_actual_size := ReadInteger( “uint”, &buffer_actual_size, 0 )
MsgBox, readprocessmemory: %ErrorLevel% – %result% ~ %buffer_actual_size%
; — string —
VarSetCapacity( buffer3, 1000, 0 )
VarSetCapacity( buffer_actual_size, 4, 0 )
result := DllCall( “ReadProcessMemory”
                  , “uint”, hp_explorer
                  , “uint”, ReadInteger( “int”, &buffer, 16 )
                  , “uint”, &buffer3
                  , “uint”, 1000
                  , “uint”, &buffer_actual_size )
buffer_actual_size := ReadInteger( “uint”, &buffer_actual_size, 0 )
MsgBox, readprocessmemory: %ErrorLevel% – %result% ~ %buffer_actual_size%
MsgBox, %
   ( Join
      “– [” A_Index-1 “] button information –”
      “`n`ncommand = ” ReadInteger( “int”, &buffer, 4 )
      “`nstate = ” ReadInteger( “uchar”, &buffer, 8 )
      “`ndata = ” ReadInteger( “uint”, &buffer, 12 )
      “`nstring = ” ReadInteger( “int”, &buffer, 16 )
      “`n`nrect = ” ReadInteger( “int”, pBuffer2, 0, false )
         “, ” ReadInteger( “int”, pBuffer2, 4, false )
         “, ” ReadInteger( “int”, pBuffer2, 8, false )
         “, ” ReadInteger( “int”, pBuffer2, 12, false )
      “`n`ndata = ” ReadInteger( “uint”, &buffer2, 0 )
      “`nstring = ” ReadStringA( &buffer3, 0, 1000 )
   )
ExitApp
TwosComplement( p_value, p_size )
{
   return, ( p_value^( 2**( p_size*8 )-1 ) )+1
}
ReadInteger( p_type, p_address, p_offset, p_hex=true )
{
   old_FormatInteger := a_FormatInteger
   if ( p_hex )
      SetFormat, integer, hex
   else
      SetFormat, integer, dec
   if ( p_type = “int” )
   {
      sign := true
      size = 4
   }
   else if ( p_type = “uint” )
   {
      sign := false
      size = 4
   }
   else if ( p_type = “short” )
   {
      sign := true
      size = 2
   }
   else if ( p_type = “ushort” )
   {
      sign := false
      size = 2
   }
   else if ( p_type = “char” )
   {
      sign := true
      size = 1
   }
   else if ( p_type = “uchar” )
   {
      sign := false
      size = 1
   }
   else
      MsgBox, [ReadInteger] error: the type %p_type% is undefined!
   value = 0
   loop, %size%
      value := value+( *( ( p_address+p_offset )+( a_Index-1 ) ) << ( 8*( a_Index-1 ) ) )
   if ( sign )
   {
      sign := ( *( p_address+p_offset+( size-1 ) ) ) >> 7
      if ( sign )
         value := -1*TwosComplement( value, size )
   }
   SetFormat, integer, %old_FormatInteger%
   return, value
}
ReadStringA( p_address, p_offset, p_size )
{
   text=
   address := p_address+p_offset-2
   loop, %p_size%
   {
      address += 2
      if ( *( address ) = 0 )
         break
      text := text Chr( *( address ) )
   }
   return, text
}
FindWindow( p_tree )
{
   level_total = 0
   loop, parse, p_tree, |
   {
      level_total++
      ix := InStr( a_LoopField, “,” )
      if ( ix )
      {
         StringMid, tree[%level_total%]?class, a_LoopField, 1, ix-1
         StringMid, tree[%level_total%]?title, a_LoopField, ix+1, StrLen( a_LoopField )-ix
      }
      else
      {
         tree[%level_total%]?class := a_LoopField
         tree[%level_total%]?title = 0
      }
   }
   hw_parent = 0
   hw_child = 0
   level = 1
   loop,
   {
      hw_child := FindWindowEx( hw_parent, hw_child, tree[%level%]?class, tree[%level%]?title )
      if ( hw_child )
      {
         if ( level = level_total )
            return, hw_child
         level++
         hw_parent_old := hw_parent
         hw_parent := hw_child
         hw_child_old := hw_child   
         hw_child = 0
      }
      else
      {
         if ( level = 1 )
            return, 0
         level–
         hw_parent := hw_parent_old
         hw_child := hw_child_old
      }
   }
}
FindWindowEx( p_hw_parent, p_hw_child, p_class, p_title=0 )
{
   if ( p_title = 0 )
      type_title = uint
   else
      type_title = str
   return, DllCall( “FindWindowEx”
                  , “uint”, p_hw_parent
                  , “uint”, p_hw_child
                  , “str”, p_class
                  , type_title, p_title )
}

 

; output is kind like

;– [-1] button information —
;command = 0xc
;state = 0x4
;data = 0x16ec80
;string = 0x120528
;rect = 0, 0, 18, 18
;data = 0x4053c
;string = ActivateTrayIcon2.ahk

3) communicate with a trayICON from pid

;Assign the PID of interest to “pid_target” and run the script.

 

#NoTrayIcon
DetectHiddenWindows, On
OnExit, HandleExit
pid_target = 2532
hw_notification_area := FindWindow( “Shell_TrayWnd|TrayNotifyWnd|SysPager|ToolbarWindow32,Notification Area” )
if ( ErrorLevel or ! hw_notification_area )
{
   MsgBox, [error] FindWindow failed: ToolbarWindow32,Notification Area
   ExitApp
}
WinGet, pid_explorer, PID, ahk_id %hw_notification_area%
if ( ErrorLevel or ! pid_explorer )
{
   MsgBox, [error] WinGet~PID failed: explorer
   ExitApp
}
hp_explorer := DllCall( “OpenProcess”
                     , “uint”, 0x18                              ; PROCESS_VM_OPERATION|PROCESS_VM_READ
                     , “int”, false
                     , “uint”, pid_explorer )
if ( ErrorLevel or ! hp_explorer )
{
   MsgBox, [error] OpenProcess failed: explorer
   ExitApp
}
remote_buffer_size = 0x1000
remote_buffer := DllCall( “VirtualAllocEx”
                     , “uint”, hp_explorer
                     , “uint”, 0
                     , “uint”, remote_buffer_size
                     , “uint”, 0x1000                           ; MEM_COMMIT
                     , “uint”, 0x4 )                              ; PAGE_READWRITE
if ( ErrorLevel or ! remote_buffer )
{
   MsgBox, [error] VirtualAllocEx failed: explorer ~ remote_buffer
   ExitApp
}
; TB_BUTTONCOUNT
SendMessage, 0x418, 0, 0,, ahk_id %hw_notification_area%
button_count := ErrorLevel
buffer_size = 40
VarSetCapacity( buffer, buffer_size )
loop, %button_count%
{
   ; TB_GETBUTTON
   SendMessage, 0x417, A_Index-1, remote_buffer,, ahk_id %hw_notification_area%
   if ( ! ErrorLevel )
   {
      MsgBox, [error] SendMessage~TB_GETBUTTON failed: hw_notification_area
      ExitApp
   }
   result := DllCall( “ReadProcessMemory”
                  , “uint”, hp_explorer
                  , “uint”, remote_buffer
                  , “uint”, &buffer
                  , “uint”, buffer_size
                  , “uint”, 0 )
   if ( ErrorLevel or ! result )
   {
      MsgBox, [error] ReadProcessMemory failed: explorer ~ remote_buffer (TB_GETBUTTON)
      ExitApp
   }
   data_address := *( &buffer+12 )+( ( *( &buffer+13 ) ) << 8 )+( ( *( &buffer+14 ) ) << 16 )+( ( *( &buffer+15 ) ) << 24 )
   result := DllCall( “ReadProcessMemory”
                     , “uint”, hp_explorer
                     , “uint”, data_address
                     , “uint”, &buffer
                     , “uint”, buffer_size
                     , “uint”, 0 )
   if ( ErrorLevel or ! result )
   {
      MsgBox, [error] ReadProcessMemory failed: explorer ~ data_address
      ExitApp
   }
   wid := *( &buffer )+( ( *( &buffer+1 ) ) << 8 )+( ( *( &buffer+2 ) ) << 16 )+( ( *( &buffer+3 ) ) << 24 )
   WinGet, pid, PID, ahk_id %wid%
   if ( pid = pid_target )
   {
      ; TB_GETITEMRECT
      SendMessage, 0x41D, A_Index-1, remote_buffer,, ahk_id %hw_notification_area%
      if ( ! ErrorLevel )
      {
         MsgBox, [error] SendMessage~TB_GETITEMRECT failed: hw_notification_area
         ExitApp
      }
      result := DllCall( “ReadProcessMemory”
                        , “uint”, hp_explorer
                        , “uint”, remote_buffer
                        , “uint”, &buffer
                        , “uint”, buffer_size
                        , “uint”, 0 )
      if ( ErrorLevel or ! result )
      {
         MsgBox, [error] ReadProcessMemory failed: explorer ~ remote_buffer (TB_GETITEMRECT)
         ExitApp
      }
      WinGetPos, x, y,,, ahk_id %hw_notification_area%
      x := x+*( &buffer )+( ( *( &buffer+1 ) ) << 8 )+( ( *( &buffer+2 ) ) << 16 )+( ( *( &buffer+3 ) ) << 24 )+8
      y := y+*( &buffer+4 )+( ( *( &buffer+5 ) ) << 8 )+( ( *( &buffer+6 ) ) << 16 )+( ( *( &buffer+7 ) ) << 24 )+8
      CoordMode, Mouse, Screen
      MouseClick, Right, x, y, 1, 0
      break
   }
}
return

HandleExit:
   result := DllCall( “VirtualFreeEx”
                     , “uint”, hp_explorer
                     , “uint”, remote_buffer
                     , “uint”, 0
                     , “uint”, 0x8000 )                           ; MEM_RELEASE
   if ( ErrorLevel or ! result )
      MsgBox, [warning] VirtualFreeEx failed: explorer ~ remote_buffer
   result := DllCall( “CloseHandle”, “uint”, hp_explorer )
   if ( ErrorLevel or ! result )
      MsgBox, [warning] CloseHandle failed: explorer
ExitApp
FindWindow( p_tree )
{
   level_total = 0
   loop, parse, p_tree, |
   {
      level_total++
      ix := InStr( a_LoopField, “,” )
      if ( ix )
      {
         StringMid, tree[%level_total%]?class, a_LoopField, 1, ix-1
         StringMid, tree[%level_total%]?title, a_LoopField, ix+1, StrLen( a_LoopField )-ix
      }
      else
      {
         tree[%level_total%]?class := a_LoopField
         tree[%level_total%]?title = 0
      }
   }
   hw_parent = 0
   hw_child = 0
   level = 1
   loop,
   {
      hw_child := FindWindowEx( hw_parent, hw_child, tree[%level%]?class, tree[%level%]?title )
      if ( hw_child )
      {
         if ( level = level_total )
            return, hw_child
         level++
         hw_parent_old := hw_parent
         hw_parent := hw_child
         hw_child_old := hw_child   
         hw_child = 0
      }
      else
      {
         if ( level = 1 )
            return, 0
         level–
         hw_parent := hw_parent_old
         hw_child := hw_child_old
      }
   }
}
FindWindowEx( p_hw_parent, p_hw_child, p_class, p_title=0 )
{
   if ( p_title = 0 )
      type_title = uint
   else
      type_title = str
   return, DllCall( “FindWindowEx”
                  , “uint”, p_hw_parent
                  , “uint”, p_hw_child
                  , “str”, p_class
                  , type_title, p_title )
}

 

4)  mouseover a trayicon

 

#persistent
Menu, Tray, Icon, pifmgr.dll, 2 ; set the icon for this script
; the icon to look for (in this case the one of this script)
iconstr = 2 pifmgr.dll ; the number of the icon [space] filename containing the icon
; get the icon sizes (system small) in order to determine the size of the area for the mouseover event
sysget, sm_cxsmicon, 49
sysget, sm_cysmicon, 50
; MsgBox, %sm_cxsmicon% %sm_cysmicon%
SetTimer, mouseovericon, 500 ; check every half second if the mouse is over the icon
mouseovericon:
   mouseovericon := iconfind(iconstr,sm_cxsmicon,sm_cysmicon)
   if mouseovericon = 0
      MsgBox Now! ; display now! when the mouse is over the icon
return
iconfind(iconstr,sm_cxsmicon,sm_cysmicon)
; checks to see if the mouse is over the given tray icon
; returns:
; 0 if mouse is over the icon,
; 1 if image not found
; 2 if imagesearch produced an error or
; 3 if icon was found but mouse was not over the image
{
   coordmode, pixel, screen ; set the coords for imagesearch to absolute
   coordmode, mouse, screen ; set mouse coords to absolute
   ; retrieve the tray position and size in order to limit (speedup) the imagesearch
   WinGetPos, trayX, trayY, trayWidth, trayHeight, ahk_class Shell_TrayWnd
   MouseGetPos, mouseX, mouseY ; get the mouse position
   ; see if the icon exists in the traywindow
   ImageSearch, imageX, imageY, trayX, trayY, trayX+trayWidth, trayY+trayHeight, *Icon%iconstr%
   coordmode, pixel, relative ; reset the coords for imagesearch to default
   coordmode, mouse, relative ; reset mouse coords to default
   if ErrorLevel > 0 ; image not found or an image search error occurred
      return %errorlevel%
   imageXlarge := imageX+sm_cxsmicon ; the outerlimit for X
   imageYlarge := ImageY+sm_cysmicon ; the outerlimit for Y
   if mouseX between %imageX% and %imageXlarge%
      if mouseY between %imageY% and %imageYlarge%
         return 0 ; if the mouse is over the designated icon
   return 3 ; 3 means mouse not over the icon
}

 

5) activate an application and simulate close action

 

SetKeyDelay,10
WinShow,ahk_class ZAFrameWnd
WinActivate,ahk_class ZAFrameWnd
WinWaitActive,ahk_class ZAFrameWnd,,0
if errorlevel != 1
 {
  Send, !{Space}
  Send SS
  Send {Enter 2}
 }

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: