Jump to content
Sign in to follow this  

Right click context anywhere?

Recommended Posts


Is there a way to make it so you can right click anywhere in a autoit window (GUI that you made) and the context menu will pop up. When you have pictures and words and clickboxes everywhere, you have to click in a sertin spot to get the menu to popup? Would there be a way to do that?

Share this post

Link to post
Share on other sites

From help file:


After creating the context menu main control with this function, each menu item can be created by using GUICtrlCreateMenuItem.

Sub-menus can be created using GUICtrlCreateMenu.

If you use no parameters or -1 in this function then the context menu that is created is associated with the entire GUI window rather than an individual control.

Only one context menu per control is possible. If you wish to create a new context menu one you have to delete the existing menu first.

Note: You can't create context menus for controls that already have system context menus, i.e. edit or input controls.

Still, right-clicking on a control might not give you the context menu. You'll have to create your own menu. This is an example from GUICtrlCreateContextMenu() at the help file:

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

Opt('MustDeclareVars', 1)


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

You can modify it to your needs.

Share this post

Link to post
Share on other sites



#include <GuiConstantsEx.au3>

$hGUI = GUICreate("Test", 200, 100)

$GUIMenu = GUICtrlCreateContextMenu(-1)
$GUIMenu_New = GUICtrlCreateMenuItem("New", $GUIMenu)
$GUIMenu_Open = GUICtrlCreateMenuItem("Open", $GUIMenu)
$GUIMenu_Close = GUICtrlCreateMenuItem("Close", $GUIMenu)

$OptionsButton = GUICtrlCreateButton("Options", 11, 37, 75, 23)
$OptionsMenu = GUICtrlCreateContextMenu($OptionsButton)
$OptionsMenu_Global = GUICtrlCreateMenuItem("Global options", $OptionsMenu)
$OptionsMenu_Local = GUICtrlCreateMenuItem("Local options", $OptionsMenu)

$HelpButton = GUICtrlCreateButton("Help", 116, 37, 75, 23)
$HelpMenu = GUICtrlCreateContextMenu($HelpButton)
$HelpMenu_Help = GUICtrlCreateMenuItem("Help", $HelpMenu)
$HelpMenu_About = GUICtrlCreateMenuItem("About", $HelpMenu)



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