Sign in to follow this  
Followers 0
sfresher

How to select an item from a pop-up right click menu?

10 posts in this topic

#1 ·  Posted (edited)

I have managed to right click on the program's system tray icon, and its menu pops up. But after that, I really don't know how to select an item (e.g. "Stay on top") from the menu. I tried use WinMenuSelectItem, but it doesn't work. The Autoit window info tool doesn't show up a complete control info of the right click menu.

Edited by sfresher

Share this post


Link to post
Share on other sites



In the helpfile there is everything you need.

#Include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.

$settingsitem   = TrayCreateMenu("Settings")
$displayitem    = TrayCreateItem("Display", $settingsitem)
$printeritem    = TrayCreateItem("Printer", $settingsitem)
TrayCreateItem("")
$aboutitem      = TrayCreateItem("About")
TrayCreateItem("")
$exititem       = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $aboutitem
            Msgbox(64,"about:","AutoIt3-Tray-sample")
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd

Exit

Best Regards

Share this post


Link to post
Share on other sites

In the helpfile there is everything you need.

#Include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.

$settingsitem   = TrayCreateMenu("Settings")
$displayitem    = TrayCreateItem("Display", $settingsitem)
$printeritem    = TrayCreateItem("Printer", $settingsitem)
TrayCreateItem("")
$aboutitem      = TrayCreateItem("About")
TrayCreateItem("")
$exititem       = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $aboutitem
            Msgbox(64,"about:","AutoIt3-Tray-sample")
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd

Exit

Best Regards

Share this post


Link to post
Share on other sites

Thanks! I have seen this one in the sample file. However, it works differently than my app.

The sample code creates a GUI systray icon, with right click menu.

My app is trying to automate right click on an existing app, and then select an item from the menu.

In the helpfile there is everything you need.

#Include <Constants.au3>
#NoTrayIcon

Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.

$settingsitem   = TrayCreateMenu("Settings")
$displayitem    = TrayCreateItem("Display", $settingsitem)
$printeritem    = TrayCreateItem("Printer", $settingsitem)
TrayCreateItem("")
$aboutitem      = TrayCreateItem("About")
TrayCreateItem("")
$exititem       = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $aboutitem
            Msgbox(64,"about:","AutoIt3-Tray-sample")
        Case $msg = $exititem
            ExitLoop
    EndSelect
WEnd

Exit

Best Regards

Share this post


Link to post
Share on other sites

bump, please help.

Share this post


Link to post
Share on other sites

bump, please help.

Once the menu is displayed you can Send("{DOWN}") the relevant number of times, or send the underlined letter, or the letter at the beginning of the line, then Send("{ENTER}")

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

This way surely works. But it cannot interact with the item on the right click menu.

For example, how can I read the each item from the menu? How can I select the item based on its text or index?

Once the menu is displayed you can Send("{DOWN}") the relevant number of times, or send the underlined letter, or the letter at the beginning of the line, then Send("{ENTER}")

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

This way surely works. But it cannot interact with the item on the right click menu.

For example, how can I read the each item from the menu? How can I select the item based on its text or index?

@sfresher

here's a rough example based on 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

enjoy

note: not setup for menu subitems

#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
Edited by rover

I see fascists...

Share this post


Link to post
Share on other sites

VERY nice and usefull translation of A3L example!

Thanks Zedna,

I gutted the unused functions from the example

and customized it a bit more for menus only.

the tooltip and toolbar examples could use updating as well.

maybe I'll post this in the example forum.


I see fascists...

Share this post


Link to post
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
Sign in to follow this  
Followers 0