GUICtrlCreateContextMenu
创建控件或 GUI 的上下文菜单主控件.
GUICtrlCreateContextMenu ( [controlID] )
参数
返回值
成功:
|
返回控件标识符(控件ID).
|
失败:
|
返回 0.
|
备注
使用本函数创建右键菜单的主控件, 每个菜单项使用 GUICtrlCreateMenuItem() 函数创建.
子菜单则使用 GUICtrlCreateMenu() 函数创建.
若参数为空或 -1, 则创建的右键菜单将关联整个 GUI 窗口, 而不是个别控件.
每个控件只能设置一个右键菜单. 因此要创建新的右键菜单则必须先删除现有的菜单.
注意: 不能为本身已有系统右键菜单的控件创建右键菜单, 比如 编辑框/输入控件等.
相关
GUICtrlCreateMenu, GUICtrlCreateMenuItem, GUICtrlDelete, GUICtrlGetHandle, GUICtrlSetState
函数示例
示例 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
示例 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
----------------------------------------
|