[已解决] GUICtrlCreateMenu 如何自定义 ‘打开’ 坐标位置
本帖最后由 zpmc123 于 2017-3-30 12:53 编辑如何自定义 ‘打开’ 坐标位置,比如把它放在中间,或者是下面
注释一下,谢谢了
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
GUICreate("AU3",300,200)
$M1 = GUICtrlCreateMenu("打开")
GUICtrlSetColor($m1,0xff0000)
$aa = GUICtrlCreateMenuItem("仅为示例",$M1)
GUICtrlSetColor($aa,0xff0000)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
If $msg = $gui_event_close Then Exit
WEnd 需要的位置创建个 Label,再 GUICtrlCreateContextMenu
给它创建个弹出菜单不就行了 回复 2# afan
谢谢AFAN老大的提醒
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
GUICreate("AU3",300,200)
$Label1 = GUICtrlCreateLabel("Label1", 18, 160, 84, 25)
GUICtrlSetColor($Label1,0xff0000)
$aa = GUICtrlCreateContextMenu("仅为示例",$Label1)
GUICtrlSetColor($aa,0xff0000)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
If $msg = $gui_event_close Then Exit
WEnd GUICreate("AU3",300,200)
$Label1 = GUICtrlCreateLabel("右键菜单", 18, 160, 70, 15)
GUICtrlSetColor($Label1,0xff0000)
$context = GUICtrlCreateContextMenu($Label1)
$item1 = GUICtrlCreateMenuItem('菜单1', $context)
$item2 = GUICtrlCreateMenuItem('菜单2', $context)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Switch $msg
Case -3
Exit
Case $item1
MsgBox(0, '', 'ok1')
Case $item2
MsgBox(0, '', 'ok2')
EndSwitch
WEnd 本帖最后由 zpmc123 于 2017-3-30 12:50 编辑
回复 4# afan
谢谢AFAN想用左建的效果实现 #include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
Local $hGui, $OptionsBtn, $OptionsDummy, $OptionsContext, $OptionsCommon, $OptionsFile, $msg ;定义变量,常量,或创建数组
Local $OptionsExit ;定义变量,常量,或创建数组
$hGui = GUICreate("AU3", 300, 200);主窗体
$Icon3 = GUICtrlCreateIcon("H:\AU3\AU3安装包\20170318\Images\2\chk1.ico", -1, 32, 120, 16, 16);1
$Checkbox3 = GUICtrlCreateCheckbox("中国人民银行", 56, 120, 115, 17)
$aa = GUICtrlCreateLabel("左键菜单", 20, 185, 70, 20, $BS_FLAT);菜单项位置可自义
$OptionsDummy = GUICtrlCreateDummy();创建虚拟(Dummy)控件
$OptionsContext = GUICtrlCreateContextMenu($OptionsDummy);创建控件或 GUI 的上下文菜单主控件
$OptionsCommon = GUICtrlCreateMenuItem("中国人民解放军", $OptionsContext);子菜单项
$OptionsFile = GUICtrlCreateMenuItem("中国人民银行", $OptionsContext);子菜单项
GUICtrlCreateMenuItem("", $OptionsContext) ;行隔断符长线,放在什么地方就是什么地方的分界线
$OptionsExit = GUICtrlCreateMenuItem("退出示例", $OptionsContext);子菜单项 要修改的选项变量,常量,或数组
GUISetState()
While 1
$msg = GUIGetMsg()
Switch $msg
Case $OptionsExit, $GUI_EVENT_CLOSE
ExitLoop
Case $aa
ShowMenu($hGui, $msg, $OptionsContext)
EndSwitch
WEnd
GUIDelete()
Func ShowMenu($hWnd, $CtrlID, $nContextID)
Local $arPos, $x, $y
Local $hMenu = GUICtrlGetHandle($nContextID)
$arPos = ControlGetPos($hWnd, "", $CtrlID)
$x = $arPos
$y = $arPos + $arPos ;吸附位置
ClientToScreen($hWnd, $x, $y)
TrackPopupMenu($hWnd, $hMenu, $x, $y)
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
Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc左键效果,只是有时候会不在想要的范围内{:face (303):} 比帮助里的简单一些样,备用。 回复 4# afan
A大我照葫芦画瓢 不能在菜单1下面再添加子菜单,能写一下吗? 回复 8# heroxianf GUICreate("AU3",300,200)
$Label1 = GUICtrlCreateLabel("右键菜单", 18, 160, 70, 15)
GUICtrlSetColor($Label1,0xff0000)
$context = GUICtrlCreateContextMenu($Label1)
$item0 = GUICtrlCreateMenu('菜单1', $context)
$item1 = GUICtrlCreateMenuItem('1子菜单', $item0)
$item2 = GUICtrlCreateMenuItem('菜单2', $context)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Switch $msg
Case -3
Exit
Case $item1
MsgBox(0, '', 'ok1')
Case $item2
MsgBox(0, '', 'ok2')
EndSwitch
WEnd 回复 9# afan
感谢A大, 这样就又解决一个问题了。 回复 9# afan
A大能不能用这么短的代码把右键改左键呢,要实在不行就只能用上面那种了 回复 11# zpmc123
左键右键都行$hGui = GUICreate("AU3", 300, 200)
$Label1 = GUICtrlCreateLabel("左右键菜单", 18, 160, 65, 15)
GUICtrlSetColor($Label1, 0xff0000)
$context = GUICtrlCreateContextMenu($Label1)
$hMenu = GUICtrlGetHandle($context)
$item0 = GUICtrlCreateMenu('菜单1', $context)
$item1 = GUICtrlCreateMenuItem('1子菜单', $item0)
$item2 = GUICtrlCreateMenuItem('菜单2', $context)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Switch $msg
Case -3
Exit
Case $item1
MsgBox(0, '', '1子菜单')
Case $item2
MsgBox(0, '', '菜单2')
Case $Label1
Local $ap = MouseGetPos()
DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $ap, "int", $ap, "hwnd", $hGui, "ptr", 0)
EndSwitch
WEnd 学习学习{:face (396):} 这个真心不错。。学习学习
页:
[1]