Jump to content
Sign in to follow this  

How to get handle and text of context menu

Recommended Posts


Hello all,

I encounter a problem is that when press button right of mouse at any position on window, a context menu will appear beside mouse's cursor. So, how do I get the information of context menu's handle and text on it?

Thanks all you and I apprecicate if any help.

PS: I have already searched key word "get handle of context menu" on forum but still not see a correct answer for me.

Edited by hsuchister

Share this post

Link to post
Share on other sites

Modified from the help file (GUICtrlCreateContextMenu) look at the === :

#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>

Opt('MustDeclareVars', 1)


; ****************
; * First sample *
; ****************
Func Example1()
    Local $contextmenu, $button, $buttoncontext, $buttonitem, $msg
    Local $newsubmenu, $textitem, $fileitem, $saveitem, $infoitem

    ;right click on gui to bring up context Menu.
    ;right click on the "ok" button to bring up a controll specific context menu.

    GUICreate("My GUI Context Menu", 300, 200)

    $contextmenu = GUICtrlCreateContextMenu()

    $button = GUICtrlCreateButton("OK", 100, 100, 70, 20)
    $buttoncontext = GUICtrlCreateContextMenu($button)
    $buttonitem = GUICtrlCreateMenuItem("About button", $buttoncontext)

    $newsubmenu = GUICtrlCreateMenu("new", $contextmenu)
    $textitem = GUICtrlCreateMenuItem("text", $newsubmenu)

    $fileitem = GUICtrlCreateMenuItem("Open", $contextmenu)
    $saveitem = GUICtrlCreateMenuItem("Save", $contextmenu)
    GUICtrlCreateMenuItem("", $contextmenu)     ; separator

    $infoitem = GUICtrlCreateMenuItem("Info", $contextmenu)


    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
        If $msg = $infoitem Then MsgBox(64, "About...", "GUICtrlGetHandle-Sample") ;====================== this was you looking for ?
EndFunc   ;==>Example1

; *****************
; * Second sample *
; *****************
Func Example2()
    Local $hGui, $OptionsBtn, $OptionsDummy, $OptionsContext, $OptionsCommon, $OptionsFile, $msg
    Local $OptionsExit, $HelpBtn, $HelpDummy, $HelpContext, $HelpWWW, $HelpAbout
    $hGui = GUICreate("My GUI", 170, 40)

    $OptionsBtn = GUICtrlCreateButton("&Options", 10, 10, 70, 20, $BS_FLAT)

    ; At first create a dummy control for the options and a contextmenu for it
    $OptionsDummy = GUICtrlCreateDummy()
    $OptionsContext = GUICtrlCreateContextMenu($OptionsDummy)
    $OptionsCommon = GUICtrlCreateMenuItem("Common", $OptionsContext)
    $OptionsFile = GUICtrlCreateMenuItem("File", $OptionsContext)
    GUICtrlCreateMenuItem("", $OptionsContext)
    $OptionsExit = GUICtrlCreateMenuItem("Exit", $OptionsContext)

    $HelpBtn = GUICtrlCreateButton("&Help", 90, 10, 70, 20, $BS_FLAT)

    ; Create a dummy control and a contextmenu for the help too
    $HelpDummy = GUICtrlCreateDummy()
    $HelpContext = GUICtrlCreateContextMenu($HelpDummy)
    $HelpWWW = GUICtrlCreateMenuItem("Website", $HelpContext)
    GUICtrlCreateMenuItem("", $HelpContext)
    $HelpAbout = GUICtrlCreateMenuItem("About...", $HelpContext)


    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $OptionsExit, $GUI_EVENT_CLOSE
            Case $OptionsBtn
                ShowMenu($hGui, $msg, $OptionsContext)
            Case $HelpBtn
                ShowMenu($hGui, $msg, $HelpContext)
            Case $HelpAbout
                MsgBox(64, "About...", "GUICtrlGetHandle-Sample")
EndFunc   ;==>Example2

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $CtrlID, $nContextID)
    Local $arPos, $x, $y
    Local $hMenu = GUICtrlGetHandle($nContextID)
    $arPos = ControlGetPos($hWnd, "", $CtrlID)
    $x = $arPos[0]
    $y = $arPos[1] + $arPos[3]
    ClientToScreen($hWnd, $x, $y)
    TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc   ;==>ShowMenu

; Convert the client (GUI) coordinates to screen (desktop) coordinates
Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
    Local $stPoint = DllStructCreate("int;int")
    DllStructSetData($stPoint, 1, $x)
    DllStructSetData($stPoint, 2, $y)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))
    $x = DllStructGetData($stPoint, 1)
    $y = DllStructGetData($stPoint, 2)
    ; release Struct not really needed as it is a local
    $stPoint = 0
EndFunc   ;==>ClientToScreen

; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc   ;==>TrackPopupMenu

Edit : Not sure if this is what you looking for since i've read the post too fast Posted Image

Edited by Jayson

Share this post

Link to post
Share on other sites


Oh, no. I mention that the context menu is not created by our program. It is of window or 3-party company.

For example: Open notepad program, move mouse to middle of notepad's screen. After that, I press the right button of mouse, a context popup menu will be showed as below:

My link

I would like to get handle of context menu and text on it as image above.

PS: Sorry, I misunderstand why my account can not post image directly.

Edited by hsuchister

Share this post

Link to post
Share on other sites

I doubt you can do this but i'm not really good with these things so u'll have to wait for another answer/trick Posted Image

Share this post

Link to post
Share on other sites

I have already found links as below:

However, when using code of these above link, then AutoIT showes error as A3LMenu.au3 file does not open.

So, after reviewing all library file in AutoIT installed on PC, I do not find any file related A3LMenu.au3.

Could you send me the library files in Auto3Lib that PaulIA write out?

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  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.