Jump to content
Sign in to follow this  

reusing a context menu

Recommended Posts


Hey all,

I've been figuring a lot more stuff out with autoit thanks to this awesome community :P

I have a problem now though where I'll need a context menu on several buttons with the same options, calling the same functions

so I was wondering if there's an elegant way to reuse or refrence the same context menu several times within a window or several different controls?


Edited by FunkyBunnies

Share this post

Link to post
Share on other sites

Does this help at all?

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

$hGui = GUICreate("GUI", 250, 40)

$Button_1 = GUICtrlCreateButton("1", 10, 10, 70, 20)
$Button_2 = GUICtrlCreateButton("2", 90, 10, 70, 20)
$Button_3 = GUICtrlCreateButton("3", 170, 10, 70, 20)

$OptionsDummy = GUICtrlCreateDummy()
$OptionsContext = GUICtrlCreateContextMenu($OptionsDummy)
$Item1 = GUICtrlCreateMenuItem("Item1", $OptionsContext)
$Item2 = GUICtrlCreateMenuItem("Item2", $OptionsContext)
$Item3 = GUICtrlCreateMenuItem("Item3", $OptionsContext)


While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
        Case $Button_1, $Button_2, $Button_3
            ShowMenu($hGui, $msg, $OptionsContext)

; 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[0]
    $y = $arPos[1] + $arPos[3]
    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

; 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)

Share this post

Link to post
Share on other sites

awesome, it shouldn't be hard to alter this for my needs

thanks a bunch, foster74 :P

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this