Creates a context menu for a control or entire GUI window.
GUICtrlCreateContextMenu ( [controlID] )
controlID | [optional] Control identifier as returned by a GUICtrlCreate...() function. |
Success: | the identifier (controlID) of the new control. |
Failure: | 0. |
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.
GUICtrlCreateMenu, GUICtrlCreateMenuItem, GUICtrlDelete, GUICtrlGetHandle, GUICtrlSetState
; right click on gui to bring up context Menu.
; right click on the "ok" button to bring up a controll specific context menu.
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
Example()
Func Example()
GUICreate("My GUI Context Menu", 300, 200)
Local $idContextmenu = GUICtrlCreateContextMenu()
Local $idMnu_Newsub = GUICtrlCreateMenu("new", $idContextmenu)
Local $idMni_NewsubmenuText = GUICtrlCreateMenuItem("text", $idMnu_Newsub)
Local $idButton = GUICtrlCreateButton("OK", 100, 100, 70, 20)
Local $idCtx_Button = GUICtrlCreateContextMenu($idButton)
Local $idMni_About = GUICtrlCreateMenuItem("About button", $idCtx_Button)
Local $idMni_Open = GUICtrlCreateMenuItem("Open", $idContextmenu)
Local $idMni_Save = GUICtrlCreateMenuItem("Save", $idContextmenu)
GUICtrlCreateMenuItem("", $idContextmenu) ; separator
Local $idMni_Info = 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 $idMni_About
MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'About')
Case $idMni_Open
MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Open')
Case $idMni_Save
MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Save')
Case $idMni_Info
MsgBox($MB_SYSTEMMODAL, "Menu Selected", 'Info')
Case $idMni_NewsubmenuText
MsgBox($MB_SYSTEMMODAL, "SubMenu Selected", 'Text')
EndSwitch
WEnd
GUIDelete()
EndFunc ;==>Example
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
Example()
Func Example()
Local $hGui = GUICreate("My GUI", 170, 40)
Local $idBtn_Options = GUICtrlCreateButton("&Options", 10, 10, 70, 20, $BS_FLAT)
; At first create a dummy control for the options and a contextmenu for it
Local $idDmy_Options = GUICtrlCreateDummy()
Local $idCtx_Options = GUICtrlCreateContextMenu($idDmy_Options)
GUICtrlCreateMenuItem("Common", $idCtx_Options)
GUICtrlCreateMenuItem("File", $idCtx_Options)
GUICtrlCreateMenuItem("", $idCtx_Options)
Local $idMni_OptionsExit = GUICtrlCreateMenuItem("Exit", $idCtx_Options)
Local $idBtn_Help = GUICtrlCreateButton("&Help", 90, 10, 70, 20, $BS_FLAT)
; Create a dummy control and a contextmenu for the help too
Local $idDmy_Help = GUICtrlCreateDummy()
Local $idCtx_Help = GUICtrlCreateContextMenu($idDmy_Help)
GUICtrlCreateMenuItem("Website", $idCtx_Help)
GUICtrlCreateMenuItem("", $idCtx_Help)
Local $idMni_HelpAbout = GUICtrlCreateMenuItem("About...", $idCtx_Help)
GUISetState(@SW_SHOW)
Local $idMsg
; Loop until the user exits.
While 1
$idMsg = GUIGetMsg()
Switch $idMsg
Case $idMni_OptionsExit, $GUI_EVENT_CLOSE
ExitLoop
Case $idBtn_Options
ShowMenu($hGui, $idMsg, $idCtx_Options)
Case $idBtn_Help
ShowMenu($hGui, $idMsg, $idCtx_Help)
Case $idMni_HelpAbout
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