#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WindowsConstants.au3>
Opt("GUICoordMode", 1)
$hGUI = GUICreate("Menu", 400, 300)
Global Const $idButton = GUICtrlCreateButton("Test", 100, 100, 80, 40)
Global Enum $idNew = 1000, $idOpen, $idClose, $idExit
Global $hMenu
$hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem ($hMenu, 0, "新建文件", $idNew)
_GUICtrlMenu_InsertMenuItem ($hMenu, 1, "", 0)
_GUICtrlMenu_InsertMenuItem ($hMenu, 2, "打开文件", $idOpen)
_GUICtrlMenu_InsertMenuItem ($hMenu, 3, "关闭文件", $idClose)
_GUICtrlMenu_InsertMenuItem ($hMenu, 4, "", 0)
_GUICtrlMenu_InsertMenuItem ($hMenu, 5, "退出", $idExit)
GUISetState()
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
While 1
$msg = GUIGetMsg()
Switch $msg
Case $GUI_EVENT_CLOSE
_GUICtrlMenu_DestroyMenu($hMenu)
GUIDelete()
Exit
Case $idButton
ShowMenu($hGUI)
EndSwitch
WEnd
Func New()
MsgBox(0, "信息", "新建文件")
EndFunc
Func Open()
MsgBox(0, "信息", "打开")
EndFunc
Func Close()
MsgBox(0, "信息", "关闭")
EndFunc
Func ShowMenu($hWnd)
Local $aButton = ControlGetPos("", "", $idButton)
Local $x = $aButton[0]
Local $y = $aButton[1] + $aButton[3]
ClientToScreen($hGUI, $x, $y)
_GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd, $x, $y)
Return True
EndFunc ;==>WM_CONTEXTMENU
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
Switch $iwParam
Case $idNew
New()
Case $idOpen
Open()
Case $idClose
Close()
Case $idExit
Exit
EndSwitch
EndFunc
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)
$stPoint = 0
EndFunc ;==>ClientToScreen
|