#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#Include <GuiStatusBar.au3>
$hGui = GUICreate("My GUI", 300, 100)
$OptionsBtn = GUICtrlCreateButton("按钮选项", 10, 10)
$hButton = GUICtrlGetHandle($OptionsBtn)
$OptionsContext = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
$Options1 = GUICtrlCreateMenuItem("选项1", $OptionsContext)
$Options2 = GUICtrlCreateMenuItem("选项2", $OptionsContext)
GUICtrlCreateMenuItem("", $OptionsContext)
$OptionsExit = GUICtrlCreateMenuItem("退出", $OptionsContext)
$hStatusBar = _GUICtrlStatusBar_Create($hGui)
_GUICtrlStatusBar_EmbedControl($hStatusBar, 0, $hButton,4)
GUISetState()
While 1
$msg = GUIGetMsg()
Switch $msg
Case $OptionsExit, $GUI_EVENT_CLOSE
ExitLoop
Case $OptionsBtn
ShowMenu($hGui, $msg, $OptionsContext)
Case $Options1
MsgBox(64, "点击", "点击选项1")
Case $Options2
MsgBox(64, "点击", "点击选项2")
EndSwitch
WEnd
GUIDelete()
Func ShowMenu($hWnd, $CtrlID, $nContextID)
Local $arPos, $x, $y
Local $hMenu = GUICtrlGetHandle($nContextID)
$arPos = ControlGetPos($hWnd, "", $CtrlID)
$x = $arPos[0]
$y = $arPos[1] + $arPos[3]
ClientToScreen($hWnd, $x, $y)
TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc ;==>ShowMenu
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)
; release Struct not really needed as it is a local
$stPoint = 0
EndFunc ;==>ClientToScreen
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