找回密码
 加入
搜索
查看: 2022|回复: 13

[AU3基础] [已解决] GUICtrlCreateMenu 如何自定义 ‘打开’ 坐标位置

  [复制链接]
发表于 2017-3-29 14:24:33 | 显示全部楼层 |阅读模式
本帖最后由 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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2017-3-29 16:38:58 | 显示全部楼层
需要的位置创建个 Label,再 GUICtrlCreateContextMenu
给它创建个弹出菜单不就行了
 楼主| 发表于 2017-3-29 19:28:42 | 显示全部楼层
回复 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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2017-3-29 19:58:21 | 显示全部楼层
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
 楼主| 发表于 2017-3-29 20:17:56 | 显示全部楼层
本帖最后由 zpmc123 于 2017-3-30 12:50 编辑

回复 4# afan


  谢谢AFAN  想用左建的效果实现
 楼主| 发表于 2017-3-30 12:51:29 | 显示全部楼层
#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[0]
        $y = $arPos[1] + $arPos[3]   ;吸附位置[3]
        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
左键效果,只是有时候会不在想要的范围内
发表于 2017-3-30 14:31:20 | 显示全部楼层
比帮助里的简单一些样,备用。
发表于 2017-3-30 18:00:46 | 显示全部楼层
回复 4# afan

A大  我照葫芦画瓢 不能在菜单1下面再添加子菜单,能写一下吗?
发表于 2017-3-30 18:11:39 | 显示全部楼层
回复 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
发表于 2017-3-30 18:28:29 | 显示全部楼层
回复 9# afan

感谢A大,   这样就又解决一个问题了。
 楼主| 发表于 2017-3-30 21:23:22 | 显示全部楼层
回复 9# afan


    A大能不能用这么短的代码把右键改左键呢,要实在不行就只能用上面那种了
发表于 2017-3-30 22:17:53 | 显示全部楼层
回复 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[0], "int", $ap[1], "hwnd", $hGui, "ptr", 0)
        EndSwitch
WEnd
发表于 2017-3-30 22:39:06 | 显示全部楼层
学习学习
发表于 2017-3-31 08:58:52 | 显示全部楼层
这个真心不错。。学习学习
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-20 12:09 , Processed in 0.088941 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表