Jump to content

Event Mode Context Menu Func not getting invoked?


Mbee
 Share

Recommended Posts

OK, I'm trying to use Context Menus and Context Menu Items with an Event Mode GUI, but the handling function(s) don't seem to ever be invoked.  Here's an example wherein the context menu handler ("ContextClick") never gets invoked (please ignore what appears to be unduly complex code -- I've extracted a small part of my apps code to produce this example):

Opt("MustDeclareVars", 1) ; Variables must be declared before use
Opt("GUIOnEventMode", 1) ; Use 'On Event Mode' to handle GUI controls

#include <ButtonConstants.au3>
#include <GUIConstants.au3>
#include <GuiButton.au3>

Opt("GUIOnEventMode", 1)

Global $G_GUI = GUICreate ("Context Menu Test", 300, 300, -1,-1,$WS_SYSMENU,$WS_EX_APPWINDOW)
Local $Lf_Error = @error
Local $Lf_Ext = @extended

GUISetOnEvent($GUI_EVENT_CLOSE, "Close", $G_GUI)

Global $Gc_ItemBtnID = GUICtrlCreateButton("Button",100,100)
GUICtrlSetState($Gc_ItemBtnID,BitOr($GUI_SHOW,$GUI_ENABLE,$GUI_DEFBUTTON))
GUICtrlSetOnEvent($Gc_ItemBtnID,"NormalClick")
_MyInitContextMenus()
GUISetState()

While True
    Sleep(1000)
WEnd


Func _MyInitContextMenus()
    Const $Cn_CtxMenuItemItemStr        = "Alpha"
    Global $G_BtnCtxMenuID = GUICtrlCreateContextMenu( $Gc_ItemBtnID )
    GUICtrlSetOnEvent( $G_BtnCtxMenuID, "ContextClick" )
    Global $G_MenItmID = GUICtrlCreateMenuItem( $Cn_CtxMenuItemItemStr, $G_BtnCtxMenuID )
EndFunc

Func NormalClick()
    MsgBox($MB_OK, "Context Menu Test", "NormalClick Invoked")
EndFunc

Func _ProcessMenuItemCmd()  ; Do context menu item stuff
    MsgBox($MB_OK, "Context Menu Test", "_ProcessMenuItemCmd Invoked")
EndFunc

Func ContextClick()
    MsgBox($MB_OK, "Context Menu Test", "ContextClick Invoked")
    Opt("GUIOnEventMode", 0)                                        ; Switch to Message Loop Mode temporarily until the context menu is done
    AutoItSetOption( "GUICloseOnESC", 1 )                           ; Send "Close" event when user presses ESC
;
    Local $Lf_UserCancelled = False                                     ; Assume the user will pick an option rather than cancel
    While True
        Local $Lf_EventMsgCode = GUIGetMsg()
        Switch $Lf_EventMsgCode
            Case $GUI_EVENT_NONE
                ContinueLoop

            Case $GUI_EVENT_CLOSE
                $Lf_UserCancelled = True
                ExitLoop

            Case $G_MenItmID
                ExitLoop

        EndSwitch
    WEnd
;
    If Not $Lf_UserCancelled Then
        Switch $Lf_EventMsgCode
            Case $G_MenItmID
                _ProcessMenuItemCmd()
        EndSwitch
    EndIf
;
    AutoItSetOption( "GUICloseOnESC", 0 )                           ; Re-disable Close on ESC
    Opt("GUIOnEventMode", 1)                                        ; Return to On Event Mode
    Return
;
EndFunc

Func Close()
    GUIDelete( $G_GUI )
    Exit
EndFunc

 

So what am I doing wrong? Am I setting up the GUICtrlSetOnEvent() call wrong, or what?

Link to comment
Share on other sites

  • Developers

Don't you want that on the Context menuitem like:

Func _MyInitContextMenus()
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : _MyInitContextMenus() ' & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    Const $Cn_CtxMenuItemItemStr = "Alpha"
    Global $G_BtnCtxMenuID = GUICtrlCreateContextMenu($Gc_ItemBtnID)
    Global $G_MenItmID = GUICtrlCreateMenuItem($Cn_CtxMenuItemItemStr, $G_BtnCtxMenuID)
    GUICtrlSetOnEvent($G_MenItmID, "ContextClick")
EndFunc   ;==>_MyInitContextMenus

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Hey, Jos, thanks for responding!

I could certainly do as you suggest if my app was far simpler, but in reality I have 6 to 8 identical buttons, each with the same set of 4 context menu items, so I would vastly prefer a central On Event func that handles ALL context menu clicks from each button (which would then determine in a Message Loop which context menu item was invoked) rather than have 24 to 32 separate context menu item funcs!

Or are you thinking that context menus can't have On Event functions (as may be the case), and that only context menu items can have them?

What do you think?

Link to comment
Share on other sites

  • Developers

It indeed seems that the Contextmenu message isn't part of the triggered events, but you could point  all submenu items  to the same Func and do an action based on the pressed submenu which you can test with @GUI_CtrlId

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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...