Jump to content

[SOLVED] How to read menu item clicked when using _GUICtrlMenu_CreatePopup($MNS_MODELESS) ?


Irios
 Share

Recommended Posts

I have several context menus that's been built using _GUICtrlMenu_CreatePopup() and _GUICtrlMenu_AddMenuItem().

When using _GUICtrlMenu_CreatePopup() it returns the identifier of the clicked item, sure. But it blocks the main loop while the menu is open.

When using _GUICtrlMenu_CreatePopup($MNS_MODELESS) it returns immediately (of course), but I cannot figure out how/where to read the item that was clicked. Is there a Windows Message (WM) somewhere that is triggered? I've spend an entire day trying to figure out how to do this, but I'm not getting anywhere. My google fu is depleted (I must have tried like hundreds of script variations today), I'm about to smash my keyboard and just go to bed, and abandon the entire endeavor of making pretty menus and go back to using Koda and forget about using menu icons, colors,  etc.

Anyone wanna help me out with how to read the clicked item? Before I smash my keyboard, please...

 

Example script here:

 

#include <GuiMenu.au3>
#include <WinAPIError.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>


GUIRegisterMsg($WM_CONTEXTMENU, "_WM_CONTEXTMENU")

Global $idMenuItem = 10000, $counter = 0
Global $hGUI = GUICreate("Menu test", 400, 300)
Global $hListview = GUICtrlCreateListView("", 2, 2, 396, 200)
GUISetState(@SW_SHOW)

;~ Global $hMenuContext = _GUICtrlMenu_CreatePopup()
Global $hMenuContext = _GUICtrlMenu_CreatePopup($MNS_MODELESS)
_GUICtrlMenu_AddMenuItem($hMenuContext, "Menu item", $idMenuItem)


Global $hTimer = TimerInit()
Do
    If TimerDiff($hTimer)>200 Then
        $counter += 1
         ConsoleWrite($counter & " " )
         $hTimer = TimerInit()
    EndIf

Until GUIGetMsg() = $GUI_EVENT_CLOSE

Exit

Func _WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    Switch $wParam
        Case GUICtrlGetHandle($hListview)
            ConsoleWrite( @CRLF & "_GUICtrlMenu_TrackPopupMenu = " & _GUICtrlMenu_TrackPopupMenu($hMenuContext, $wParam, -1, -1, 1, 1, 2) & @CRLF)
    EndSwitch
EndFunc

 

 

Edited by Irios
solved!

863nP4W.png discord.me/autoit  (unofficial)

Link to comment
Share on other sites

Alright, after sleeping on it.... I finally managed to figure this out.

 

Here is the working solution:

 

#include <GuiMenu.au3>
#include <WinAPIError.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>


GUIRegisterMsg($WM_CONTEXTMENU, "_WM_CONTEXTMENU")
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")

Global $idMenuItem = 10000, $counter = 0
Global $hGUI = GUICreate("Menu test", 400, 300)
Global $idListview = GUICtrlCreateListView("", 2, 2, 396, 200)
Global $hListview = GUICtrlGetHandle($idListview)
GUISetState(@SW_SHOW)

Global $hMenuContext = _GUICtrlMenu_CreatePopup($MNS_MODELESS)
_GUICtrlMenu_AddMenuItem($hMenuContext, "Menu item", $idMenuItem)


Global $hTimer = TimerInit()
Do
    If TimerDiff($hTimer)>200 Then
        $counter += 1
         ConsoleWrite($counter & " " )
         $hTimer = TimerInit()
    EndIf
Until GUIGetMsg() = $GUI_EVENT_CLOSE

Exit

Func _WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    Switch $hWnd
        Case $hGUI
            Switch $wParam
                Case $hListview
                    _GUICtrlMenu_TrackPopupMenu($hMenuContext, $hWnd)
            EndSwitch
    EndSwitch
EndFunc

Func _WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    Switch $wParam
        Case $idMenuItem
            ConsoleWrite( @CRLF & "_GUICtrlMenu_TrackPopupMenu menu item clicked = " & $idMenuItem & @CRLF)
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc

 

No keyboards were smashed in the making of this script.

 

 

 

Edited by Irios
typos

863nP4W.png discord.me/autoit  (unofficial)

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

×
×
  • Create New...