基 础 函 数 参 考


GUICtrlCreateContextMenu

创建控件或 GUI 的上下文菜单.

GUICtrlCreateContextMenu ( [控件ID] )

参 数

控件ID [可选参数] 由 GUICtrlCreate... 返回的控件标识符

返 回 值

成功: 返回控件标识符(控件ID).
失败: 返回 0.

备 注

使用本函数创建右键菜单的主控件, 每个菜单项使用 GUICtrlCreateMenuItem 函数创建.
子菜单则使用 GUICtrlCreateMenu 函数创建.

若参数为空或 -1, 则创建的右键菜单将关联整个 GUI 窗口, 而不是个别控件l.

每个控件只能设置一个右键菜单. 因此要创建新的右键菜单则必须先删除现有的菜单.

注意: 不能为本身已有系统右键菜单的控件创建右键菜单, 比如 编辑框/输入控件等.

相 关 函 数

GUICtrlCreateMenuItem, GUICtrlCreateMenu, GUICtrlGetHandle, GUICtrlSetState, GUICtrlDelete

函 数 示 例


#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>

;Example1()
Example2()

; ****************
; * 示例 1 *
; ****************
Func Example1()
    ;右键点击窗口弹出右键菜单.
    ;右键点击"确定"按钮,弹出控件特定右键菜单.

    GUICreate("我的上下文菜单 GUI", 300, 200)

    Local $contextmenu = GUICtrlCreateContextMenu()

    Local $button = GUICtrlCreateButton("确定", 100, 100, 70, 20)
    Local $buttoncontext = GUICtrlCreateContextMenu($button)
    GUICtrlCreateMenuItem ("关于按钮", $buttoncontext)

    Local $newsubmenu = GUICtrlCreateMenu("新建", $contextmenu)
    GUICtrlCreateMenuItem ("文本", $newsubmenu)

    GUICtrlCreateMenuItem ("打开", $contextmenu)
    GUICtrlCreateMenuItem ("保存", $contextmenu)
    GUICtrlCreateMenuItem ("", $contextmenu) ; 分隔线

    GUICtrlCreateMenuItem ("信息", $contextmenu)

    GUISetState()

    ; 运行 GUI, 直到 GUI 被关闭
    While 1
        Local $msg = GUIGetMsg()

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
    GUIDelete()
EndFunc   ;==>Example1


; *****************
; * 示例 2 *
; *****************
Func Example2()
    Local $hGui, $OptionsBtn, $OptionsDummy, $OptionsContext, $msg
    Local $OptionsExit, $HelpBtn, $HelpDummy, $HelpContext, $HelpAbout
    $hGui = GUICreate("我的上下文菜单 GUI", 170, 40)

    $OptionsBtn = GUICtrlCreateButton("选项 &O", 10, 10, 70, 20, $BS_FLAT)

    ; 创造一个虚拟的"选项"控件,并添加左键菜单
    $OptionsDummy = GUICtrlCreateDummy()
    $OptionsContext = GUICtrlCreateContextMenu($OptionsDummy)
    GUICtrlCreateMenuItem ("常规", $OptionsContext)
    GUICtrlCreateMenuItem ("文件", $OptionsContext)
    GUICtrlCreateMenuItem ("", $OptionsContext)
    $OptionsExit = GUICtrlCreateMenuItem ("退出", $OptionsContext)


    $HelpBtn = GUICtrlCreateButton("帮助 &H", 90, 10, 70, 20, $BS_FLAT)

    ; 创造一个虚拟的"帮助"控件,并添加左键菜单
    $HelpDummy = GUICtrlCreateDummy()
    $HelpContext = GUICtrlCreateContextMenu($HelpDummy)
    GUICtrlCreateMenuItem ("网站", $HelpContext)
    GUICtrlCreateMenuItem ("", $HelpContext)
    $HelpAbout = GUICtrlCreateMenuItem ("关于...", $HelpContext)


    GUISetState()

    While 1
        $msg = GUIGetMsg()

        Switch $msg
            Case $OptionsExit, $GUI_EVENT_CLOSE
                ExitLoop

            Case $OptionsBtn
                ShowMenu($hGui, $msg, $OptionsContext)

            Case $HelpBtn
                ShowMenu($hGui, $msg, $HelpContext)

            Case $HelpAbout
                MsgBox(64, "关于...", "GUICtrlGetHandle-Sample")
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>Example2


; 显示特定 GUI 控件的菜单
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


; 转换用户(GUI)座标到屏幕(桌面)座标
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


; 在给定坐标(x,y)上显示属于给定 GUI 窗口的弹出菜单
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

provider with jb51.net (unicode)