Sign in to follow this  
Followers 0
Zohar

GUICtrlSetOnEvent with a ContextMenu

2 posts in this topic

#1 ·  Posted (edited)

Hello

I created a small function called CreateContextMenuFromArray(),

that gets 2 parameters:

1) an array of strings, which will be the MenuItems

2) a name of a function, which will be the event handler

this function creates a ContextMenu from the String Array,

and now the only thing that is not working, is the event handler.

when clicking a MenuItem in the ContextMenu, nothing happens..

why?

Local   $Accounts[4]    =["Item1","Item2","Item3","Item4"]



;Func Main()
    Local   $WindowThatHoldsTheContextMenu = GUICreate("",1,1,-10,-10)

    Local   $ContextMenu    =CreateContextMenuFromArray($Accounts,"ContextMenuHandler")

    _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ContextMenu),$WindowThatHoldsTheContextMenu)
    
    
    Sleep(4000)
;EndFunc



Func    CreateContextMenuFromArray($MenuItems,$ContextMenuHandler_Name)
    Local   $ContextMenu            =GUICtrlCreateContextMenu()
    Local   $ContextMenu_MenuItem
    
    For $i=0 To UBound($MenuItems)-1
        $ContextMenu_MenuItem   =GUICtrlCreateMenuItem($MenuItems[$i],$ContextMenu)
        GUICtrlSetOnEvent($ContextMenu_MenuItem,$ContextMenuHandler_Name)
    Next
    
    Return $ContextMenu
EndFunc


Func    ContextMenuHandler()
    Beep(2000,50)
;   MsgBox(0,"",@GUI_CTRLID)        ;also, how do I get the MenuItem's Text from the @GUI_CTRLID?
EndFunc
Edited by Zohar

Share this post


Link to post
Share on other sites



Hello

I created a small function called CreateContextMenuFromArray(),

that gets 2 parameters:

1) an array of strings, which will be the MenuItems

2) a name of a function, which will be the event handler

this function creates a ContextMenu from the String Array,

and now the only thing that is not working, is the event handler.

when clicking a MenuItem in the ContextMenu, nothing happens..

why?

Local   $Accounts[4]    =["Item1","Item2","Item3","Item4"]

;Func Main()
    Local   $WindowThatHoldsTheContextMenu = GUICreate("",1,1,-10,-10)

    Local   $ContextMenu    =CreateContextMenuFromArray($Accounts,"ContextMenuHandler")

    _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($ContextMenu),$WindowThatHoldsTheContextMenu)
    
    Sleep(4000)
;EndFunc

Func    CreateContextMenuFromArray($MenuItems,$ContextMenuHandler_Name)
    Local   $ContextMenu            =GUICtrlCreateContextMenu()
    Local   $ContextMenu_MenuItem
    
    For $i=0 To UBound($MenuItems)-1
        $ContextMenu_MenuItem   =GUICtrlCreateMenuItem($MenuItems[$i],$ContextMenu)
        GUICtrlSetOnEvent($ContextMenu_MenuItem,$ContextMenuHandler_Name)
    Next
    
    Return $ContextMenu
EndFunc

Func    ContextMenuHandler()
    Beep(2000,50)
;   MsgBox(0,"",@GUI_CTRLID)    ;also, how do I get the MenuItem's Text from the @GUI_CTRLID?
EndFunc
You might keep the control IDs and search them for matching text:
#include <GUIConstantsEx.au3>
#include <ButtonConstants.au3>
#include <Array.au3>

Opt("GuiOnEventMode", 1)

Global $contextmenu, $button, $buttoncontext, $buttonitem, $msg, $newsubmenu
Global $avMenuItems[4][2] = [["Item 1", ""], ["Item 2", ""], ["Item 3", ""], ["Item 4", ""]] 
Global $f_MenuHit = 0, $iIndex

;right click on gui to bring up context Menu.
;right click on the "ok" button to bring up a controll specific context menu.
GUICreate("My GUI Context Menu", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")

$contextmenu = GUICtrlCreateContextMenu()
$newsubmenu = GUICtrlCreateMenu("new", $contextmenu)
For $n = 0 to UBound($avMenuItems) - 1
    $avMenuItems[$n][1] = GUICtrlCreateMenuItem($avMenuItems[$n][0], $newsubmenu)
    GUICtrlSetOnEvent(-1, "_MenuHit")
Next
$button = GUICtrlCreateButton("OK", 100, 100, 70, 20)
GUICtrlSetOnEvent(-1, "_ButtonOK")
$buttoncontext = GUICtrlCreateContextMenu($button)
$buttonitem = GUICtrlCreateMenuItem("About button", $buttoncontext)
GUICtrlSetOnEvent(-1, "_ButtonAboutOK")


GUISetState()
While 1
    Sleep(20)
    If $f_MenuHit Then
        $iIndex = _ArraySearch($avMenuItems, $f_MenuHit, 0, 0, 0, 0, 1, 1)
        If @error = 0 Then
            MsgBox(64, "Menu Hit", "Menu item clicked:" & @CRLF & _
                    @TAB & "ControlID = " & $f_MenuHit & @CRLF & _
                    @TAB & "Text = " & $avMenuItems[$iIndex][0], 5)
        EndIf
        $f_MenuHit = 0
    EndIf
WEnd

Func _Quit()
    Exit
EndFunc

Func _ButtonOK()
    MsgBox(64, "OK", "OK", 2)
EndFunc

Func _ButtonAboutOK()
    MsgBox(64, "about:OK", "It's an OK button, what more can you say...?", 2)
EndFunc

Func _MenuHit()
    $f_MenuHit = @GUI_CtrlId
EndFunc

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  
Followers 0