#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
$hGUI = GUICreate("內置函數示例")
$idOK = GUICtrlCreateButton("点按钮调出菜单", 20, 50, 100, 25)
$OptionsDummy = GUICtrlCreateDummy() ;创建一个虚拟控件,让菜单依托于控件
$ContextMenu = GUICtrlCreateContextMenu($OptionsDummy) ;创造菜单
$Menu1 = GUICtrlCreateMenuItem("菜单1", $ContextMenu)
GUICtrlCreateMenuItem("", $ContextMenu)
$Menu2 = GUICtrlCreateMenuItem("菜单2", $ContextMenu)
GUISetState(@SW_SHOW, $hGUI)
; 循環到用戶退出.
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $idOK
_ShowMenu($hGUI, $idOK, $ContextMenu)
Case $Menu1
MsgBox(0, "", "你点的了菜单1")
Case $Menu2
MsgBox(0, "", "你点的了菜单2")
EndSwitch
WEnd
Func _ShowMenu($hWnd, $CtrlID, $nContextID) ; 显示一个 GUI 窗口指定矩形范围的菜单.
Opt("MouseCoordMode", 2)
$arPos = MouseGetPos()
$x = $arPos[0]
$y = $arPos[1]
ClientToScreen($hWnd, $x, $y)
TrackPopupMenu($hWnd, GUICtrlGetHandle($nContextID), $x, $y)
EndFunc ;==>ShowMenu
Func ClientToScreen($hWnd, ByRef $x, ByRef $y) ; 客户端(GUI)坐标转换为屏幕(桌面)坐标.
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
Func TrackPopupMenu($hWnd, $hMenu, $x, $y) ; 显示指定坐标(x,y), 属于一个指定 GUI 窗口(hWnd)的弹出菜单(hMenu).
DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc ;==>TrackPopupMenu