#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo
Example()
Func Example()
; 创建 GUI
GUICreate("菜单 UDF 示例", 400, 200)
$idLabel = GUICtrlCreateLabel('Label控件', 2, 2, 350, 196, BitOR($SS_CENTER,$WS_BORDER), $GUI_WS_EX_PARENTDRAG) ;$SS_CENTER 文本居中,设置$GUI_WS_EX_PARENTDRAG,按住本控件可拖动窗口
GUICtrlSetFont(-1, 16, 500, Default, '微软雅黑') ; 设置上面控件的字体.
GUICtrlSetBkColor(-1, 0x000000) ;控件的背景色
GUICtrlSetColor(-1, 0x00FF00) ;控件的文本颜色
GUISetState(@SW_SHOW)
; 注册消息处理程序
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
; 循环到用户退出.
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>Example
; 处理 WM_COMMAND 消息
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $lParam
Switch $wParam
Case $e_idOpen
_WinAPI_ShowMsg("你点击了 [打开] 菜单")
Case $e_idSave
_WinAPI_ShowMsg("你点击了 [保存] 菜单")
Case $e_idInfo
_WinAPI_ShowMsg("你点击了 [信息] 菜单")
EndSwitch
EndFunc ;==>WM_COMMAND
; 处理 WM_CONTEXTMENU 消息
Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
#forceref $hWnd, $iMsg, $lParam
Local $hMenu
$hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($hMenu, 0, "打开", $e_idOpen)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "保存", $e_idSave)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "信息", $e_idInfo)
_GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
_GUICtrlMenu_DestroyMenu($hMenu)
Return True
EndFunc ;==>WM_CONTEXTMENU
GUI显示图: