Jump to content

Enumerate tray icon menu items


rover
 Share

Recommended Posts

Enumerate tray icon menu items

A rough update of one of PaulIAs example scripts included with the old Auto3Lib add on library.

right click on tray icons to read menus,

example launches status window of Local Area Connection tray icon when tray icon right clicked on

Note: not yet setup for menu subitems properly.

when I get around to it...

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <WindowsConstants.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>
#Include <Misc.au3>

_Singleton("TrayMenuTest", 0)

Opt("MustDeclareVars", 1)
HotKeySet("{ESC}", "_Exit")

Global $gaPopups[1][3] = [[0, 0]]
Global $hMenu, $hParent

ConsoleWrite("!ESC key exits" & @CRLF)
ConsoleWrite("!Right click tray icon menu" & @CRLF)

While 1
    Sleep(100)
    _Lib_PopupScan()
    If $gaPopups[0][0] > 0 Then
        Decode()
    EndIf
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

Func Decode()
    Local $iI, $iType
    Local $ahwnd[2]

    For $iI = 1 To $gaPopups[0][0]
        $ahwnd[0] = _Lib_PopupGetHwnd($iI) ; menu hwnd
        $iType = _Lib_PopupGetType($iI) ; menu/toolbar/tooltip
        $ahwnd[1] = _Lib_PopupGetParent($iI) ; parent hwnd
        If $hMenu <> $ahwnd[0] Or $hParent <> $ahwnd[1] Then
            $hMenu = $ahwnd[0]
            $hParent = $ahwnd[1]
            If _GUICtrlMenu_IsMenu($hMenu) And IsHWnd($hParent) Then
                Select
                    Case $iType = 1
                        ShowMenu($hMenu, $hParent)
                EndSelect
            EndIf
        EndIf
    Next
   
    Return $ahwnd
EndFunc   ;==>Decode

; ===============================================================================
; Show information about a menu item
; ===============================================================================
Func ShowMenu($hMenu, $hParent)
    Local $iI, $iCount, $sItem, $aRect, $sString, $sAccel
    $iCount = _GUICtrlMenu_GetItemCount($hMenu)
    ConsoleWrite("+Parent handle .........: " & $hParent & @CRLF)
    ConsoleWrite("+Menu handle ...........: " & $hMenu & @CRLF)
    ConsoleWrite("+Menu item count .......: " & $iCount & @CRLF)
    ConsoleWrite(@CRLF)
    For $iI = 0 To $iCount - 1
        If $iI < 10 Then
            $sItem = "-Item  " & $iI & " "
        Else
            $sItem = "-Item " & $iI & " "
        EndIf
       
        $aRect = _GUICtrlMenu_GetItemRect($hParent, $hMenu, $iI)
       
        If _GUICtrlMenu_GetItemType($hMenu, $iI, True) = $MFT_SEPARATOR Then ; determine if index item is menu separator
            $sString = "Separator"
        Else
            $sString = _GUICtrlMenu_GetItemText($hMenu, $iI)
        EndIf
       
        ; find accelerator keys
        $sAccel = StringInStr($sString, "&")
        If $sAccel Then
            $sAccel = StringMid($sString, $sAccel +1, 1)
        Else
            $sAccel = ""
        EndIf
       
        ; Local Area Connection - network tray icon, launches status window - change to your preferred tray menu item
        If $sString == "&Status" And $sAccel <> "" And Send($sAccel) Then Exit

        ConsoleWrite($sItem & "string ........: " & $sString & @CRLF)
        ConsoleWrite($sItem & "accelerator ...: " & $sAccel & @CRLF)
        ConsoleWrite($sItem & "command ID ....: " & _GUICtrlMenu_GetItemID($hMenu, $iI) & @CRLF)
        ConsoleWrite($sItem & "checked .......: " & _GUICtrlMenu_GetItemChecked($hMenu, $iI) & @CRLF)
        ConsoleWrite($sItem & "disabled ......: " & _GUICtrlMenu_GetItemDisabled($hMenu, $iI) & @CRLF)
        ConsoleWrite($sItem & "grayed ........: " & _GUICtrlMenu_GetItemGrayed($hMenu, $iI) & @CRLF)
       
        If IsArray($aRect) Then
            ConsoleWrite($sItem & "rectangle .....: [" & $aRect[0] & _
                    ", " & $aRect[1] & ", " & $aRect[2] & ", " & $aRect[3] & "]" & @CRLF)
        EndIf
        ConsoleWrite(@CRLF)
    Next
EndFunc   ;==>ShowMenu

Func _Lib_PopupGetParent($iIndex = 1)
    _Lib_PopupWait()
    Return $gaPopups[$iIndex][2]
EndFunc   ;==>_Lib_PopupGetParent

Func _Lib_PopupGetType($iIndex = 1)
    _Lib_PopupWait()
    Return $gaPopups[$iIndex][1]
EndFunc   ;==>_Lib_PopupGetType

Func _Lib_PopupGetHwnd($iIndex = 1)
    _Lib_PopupWait()
    Return $gaPopups[$iIndex][0]
EndFunc   ;==>_Lib_PopupGetHwnd

Func _Lib_PopupScan()
    Local $iI, $sClass, $hWnd, $hMenu
    ReDim $gaPopups[1][3]
    $gaPopups[0][0] = 0
    ReDim $winapi_gaWinList[64][2]
    $winapi_gaWinList[0][0] = 0
    $winapi_gaWinList[0][1] = 64
    _WinAPI_EnumWindowsPopup()
    For $iI = 1 To $winapi_gaWinList[0][0]
        $hWnd = $winapi_gaWinList[$iI][0]
        $sClass = $winapi_gaWinList[$iI][1]
        Select
            Case $sClass = "#32768"
                $hMenu = _SendMessage($hWnd, $MN_GETHMENU, 0, 0)
                _Lib_PopupAdd($hMenu, 1, $hWnd)
            Case $sClass = "ToolbarWindow32"
                _Lib_PopupAdd($hWnd, 2, _WinAPI_GetParent($hWnd))
            Case $sClass = "ToolTips_Class32"
                _Lib_PopupAdd($hWnd, 3, _WinAPI_GetParent($hWnd))
        EndSelect
    Next
EndFunc   ;==>_Lib_PopupScan

Func _Lib_PopupWait()
    Local $iLoop = 0
    While $iLoop < 50
        If $gaPopups[0][0] > 0 Then Return
        Sleep(100)
        _Lib_PopupScan()
        $iLoop += 1
    WEnd
    ConsoleWrite("Timeout waiting for popup window to appear" & @CRLF)
EndFunc   ;==>_Lib_PopupWait

Func _Lib_PopupAdd($hWnd, $iType, $hParent)
    Local $iCount
    $gaPopups[0][0] += 1
    $iCount = $gaPopups[0][0]
    ReDim $gaPopups[$iCount + 1][3]
    $gaPopups[$iCount][0] = $hWnd
    $gaPopups[$iCount][1] = $iType
    $gaPopups[$iCount][2] = $hParent
EndFunc   ;==>_Lib_PopupAdd

I see fascists...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...