wanghong01 发表于 2010-7-27 09:21:43

左键弹出菜单的问题[已解决]

本帖最后由 wanghong01 于 2010-11-20 02:58 编辑

怎样使鼠标左键点击一个控件,也能象右键点击一样弹出菜单呢?{:face (382):}

afan 发表于 2010-7-27 10:21:35

帮助里有#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>

Local $hGui, $OptionsBtn, $OptionsDummy, $OptionsContext, $OptionsCommon, $OptionsFile, $msg
Local $OptionsExit, $HelpBtn, $HelpDummy, $HelpContext, $HelpWWW, $HelpAbout
$hGui = GUICreate("My GUI", 170, 40)

$OptionsBtn = GUICtrlCreateButton("&Options", 10, 10, 70, 20, $BS_FLAT)

; At first create a dummy control for the options and a contextmenu for it
$OptionsDummy = GUICtrlCreateDummy()
$OptionsContext = GUICtrlCreateContextMenu($OptionsDummy)
$OptionsCommon = GUICtrlCreateMenuItem("Common", $OptionsContext)
$OptionsFile = GUICtrlCreateMenuItem("File", $OptionsContext)
GUICtrlCreateMenuItem("", $OptionsContext)
$OptionsExit = GUICtrlCreateMenuItem("Exit", $OptionsContext)


$HelpBtn = GUICtrlCreateButton("&Help", 90, 10, 70, 20, $BS_FLAT)

; Create a dummy control and a contextmenu for the help too
$HelpDummy = GUICtrlCreateDummy()
$HelpContext = GUICtrlCreateContextMenu($HelpDummy)
$HelpWWW = GUICtrlCreateMenuItem("Website", $HelpContext)
GUICtrlCreateMenuItem("", $HelpContext)
$HelpAbout = GUICtrlCreateMenuItem("About...", $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, "About...", "GUICtrlGetHandle-Sample")
        EndSwitch
WEnd
GUIDelete()

; Show a menu in a given GUI window which belongs to a given GUI ctrl
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   ;==>ShowMenu


; Convert the client (GUI) coordinates to screen (desktop) coordinates
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


; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
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

wanghong01 发表于 2010-7-27 10:35:10

回复 2# afan
谢谢楼上的,你真是大好人啦{:face (451):} {:face (117):} {:1_415:}

ganyuansi 发表于 2017-1-1 23:24:14

东西确实在帮助里面有,可惜讲解太少了

ganyuansi 发表于 2017-1-2 12:15:48

可惜我这里来了一个按钮上弹出菜单,就如开始菜单一样的。不知道如何实现了
页: [1]
查看完整版本: 左键弹出菜单的问题[已解决]