Jump to content

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?

thanks!

Edited by FunkyBunnies
Link to comment
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)

GUISetState()

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

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


; 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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...