Function Reference


GUICtrlCreateContextMenu

Creates a context menu for a control or entire GUI window.

GUICtrlCreateContextMenu ( [controlID] )

Parameters

controlID [optional] Control identifier as returned by a GUICtrlCreate...() function.

Return Value

Success: the identifier (controlID) of the new control.
Failure: 0.

Remarks

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.

Related

GUICtrlCreateMenu, GUICtrlCreateMenuItem, GUICtrlDelete, GUICtrlGetHandle, GUICtrlSetState

Example

Example 1

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

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

Example()

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

        Local $idContextmenu = GUICtrlCreateContextMenu()

        Local $idNewsubmenu = GUICtrlCreateMenu("new", $idContextmenu)
        Local $idNewsubmenuText = GUICtrlCreateMenuItem("text", $idNewsubmenu)

        Local $idButton = GUICtrlCreateButton("OK", 100, 100, 70, 20)
        Local $idButtoncontext = GUICtrlCreateContextMenu($idButton)
        Local $idMenuAbout = GUICtrlCreateMenuItem("About button", $idButtoncontext)

        Local $idMenuOpen = GUICtrlCreateMenuItem("Open", $idContextmenu)
        Local $idMenuSave = GUICtrlCreateMenuItem("Save", $idContextmenu)
        GUICtrlCreateMenuItem("", $idContextmenu) ; separator

        Local $idMenuInfo = GUICtrlCreateMenuItem("Info", $idContextmenu)

        GUISetState(@SW_SHOW)

        ; Loop until the user exits.
        While 1
                Switch GUIGetMsg()
                        Case $GUI_EVENT_CLOSE
                                ExitLoop
                        Case $idButton
                                MsgBox($MB_SYSTEMMODAL, "Button Clicked", 'OK')
                        Case $idMenuAbout
                                MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'About')
                        Case $idMenuOpen
                                MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Open')
                        Case $idMenuSave
                                MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Save')
                        Case $idMenuInfo
                                MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Info')
                        Case $idNewsubmenuText
                                MsgBox($MB_SYSTEMMODAL, "SubMenu Selected", 'Text')
                EndSwitch
        WEnd
        GUIDelete()
EndFunc   ;==>Example

Example 2

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

Example()

Func Example()
        Local $hGui = GUICreate("My GUI", 170, 40)

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

        ; At first create a dummy control for the options and a contextmenu for it
        Local $idOptionsDummy = GUICtrlCreateDummy()
        Local $idOptionsContext = GUICtrlCreateContextMenu($idOptionsDummy)
        GUICtrlCreateMenuItem("Common", $idOptionsContext)
        GUICtrlCreateMenuItem("File", $idOptionsContext)
        GUICtrlCreateMenuItem("", $idOptionsContext)
        Local $idOptionsExit = GUICtrlCreateMenuItem("Exit", $idOptionsContext)

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

        ; Create a dummy control and a contextmenu for the help too
        Local $idHelpDummy = GUICtrlCreateDummy()
        Local $idHelpContext = GUICtrlCreateContextMenu($idHelpDummy)
        GUICtrlCreateMenuItem("Website", $idHelpContext)
        GUICtrlCreateMenuItem("", $idHelpContext)
        Local $idHelpAbout = GUICtrlCreateMenuItem("About...", $idHelpContext)

        GUISetState(@SW_SHOW)

        Local $idMsg

        ; Loop until the user exits.
        While 1
                $idMsg = GUIGetMsg()

                Switch $idMsg
                        Case $idOptionsExit, $GUI_EVENT_CLOSE
                                ExitLoop

                        Case $idOptionsBtn
                                ShowMenu($hGui, $idMsg, $idOptionsContext)

                        Case $idHelpBtn
                                ShowMenu($hGui, $idMsg, $idHelpContext)

                        Case $idHelpAbout
                                MsgBox($MB_SYSTEMMODAL, "About...", "GUICtrlGetHandle-Sample")
                EndSwitch
        WEnd
        GUIDelete()
EndFunc   ;==>Example

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $idCtrl, $idContext)
        Local $aPos, $x, $y
        Local $hMenu = GUICtrlGetHandle($idContext)

        $aPos = ControlGetPos($hWnd, "", $idCtrl)

        $x = $aPos[0]
        $y = $aPos[1] + $aPos[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 $tPoint = DllStructCreate("int;int")

        DllStructSetData($tPoint, 1, $x)
        DllStructSetData($tPoint, 2, $y)

        DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "struct*", $tPoint)

        $x = DllStructGetData($tPoint, 1)
        $y = DllStructGetData($tPoint, 2)
        ; release Struct not really needed as it is a local
        $tPoint = 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