Jump to content

Selecting a context menu item


Recommended Posts

Hi,
 
 Following my question >here, how do I select a menu item according to its text?
 
 I tried the following (please see also comments embedded):

 

If WinExists("[CLASS:#32768") Then

    $hWnd = WinGetHandle("[CLASS:#32768") ;ContextMenu
    $hMenu = _SendMessage($hWnd, $MN_GETHMENU, 0, 0)

    If _GUICtrlMenu_IsMenu($hMenu) Then
        ConsoleWrite("$menuItemText = " & $menuItemText & @CRLF) ;That is the text I am searching in the menu..

        $menuItemsCount = _GUICtrlMenu_GetItemCount($hMenu)
        ConsoleWrite("$menuItemsCount = " & $menuItemsCount & @CRLF)

        For $i = 0 To $menuItemsCount - 1 ;By the way I tried with FindItem and failed..
            $itemText = _GUICtrlMenu_GetItemText($hMenu, $i)
            ConsoleWrite("$itemText = " & $itemText & @CRLF)

            If $itemText = $menuItemText Then

                Sleep(1000)
                ConsoleWrite("{ENTER}")
                Send("{ENTER}") ;Well, this wouldn't work of course because I didn't select an item. I think I could use here something like "selectItem (text) " before the "ENTER"
                Return True
            EndIf
        Next

        ConsoleWrite("Didn't find " & $menuItemText & " in menu" & @CRLF)
        Return False
    EndIf
EndIf

 

 
Thanks!

Edited by Melba23
Added tags
Link to comment
Share on other sites

  • Moderators

dushkin,

The only way I can get it to fire is like this:

#include <WindowsConstants.au3>
#include "SendMessage.au3"
#Include <GuiMenu.au3>

Global $sRequired_Text = "Select All"

While 1

    If WinExists("[CLASS:#32768]") Then
        $hWnd = WinGetHandle("[CLASS:#32768]")
        $hMenu = _SendMessage($hWnd, $MN_GETHMENU, 0, 0)
        If _GUICtrlMenu_IsMenu($hMenu) Then
            $iCount = _GUICtrlMenu_GetItemCount($hMenu)
            For $iIndex = 0 To $iCount - 1
                Local $sText = _GUICtrlMenu_GetItemText($hMenu, $iIndex)
                If $sText = $sRequired_Text Then
                    $aPos = _GUICtrlMenu_GetItemRect($hWnd, $hMenu, $iIndex)
                    $iOldOpt = Opt("MouseCoordMode", 0)
                    MouseClick("primary", $aPos[0], $aPos[1], 1, 20) ; Adjust the speed to suit
                    Opt("MouseCoordMode", $iOldOpt)
                EndIf
            Next
        EndIf
    EndIf

    Sleep(10)

WEnd
But there must be something more elegant. I will keep looking - or maybe someone else will put us both right! :D

M23

P.S. When you post code please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can see above now I have added the tags. ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks Mel.

 Well, your code really helped me, but I had to modify the Mosue click position.

 With your code, it try to click one item above the desired and left of it, meaning it didn't really clicked the Item.

 So I modified the line to 

MouseClick("primary", $aPos[0] + 100, $aPos[1] + 20, 1, 20) 

 and then it was ok, but the question is if this is the correct way? (and it is not!)

 How to perorm this task without manualy modifying the position values?

 Thanks!

Link to comment
Share on other sites

  • Moderators

dushkin,

I have no idea why it does not click in the correct place for you - as you can see I changed the MouseCoordMode mode to the current window, which should be the popup holding the context menu. It certainly worked for me when I tried it within SciTE. What you have done is the logical way to correct the position, but I am afraid I cannot explain why it is not moving to the right place automatically. :wacko:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...