Jump to content

Context menu does not respond when opened on top of button


TeddyTech
 Share

Recommended Posts

I have a problem with a context menu. When I right click on the form the context menu opens and responds properly. (This code is based on the help file sample.)

However, when I right click on a button the context menu opens but it does not respond.

Ideally, I would like to have the context menu respond properly whenever it is opened. If that is not possible, then I want to keep the context menu from opening when right-clicking over a button.

(I have searched the forums and the help files, but cannot figure this one out.)

(And, as an aside, why does posting my code using the "AutoIt Code" option strip out the #include file names?)

Thanks!

Here is a stripped-down test version of my code:

#include <GuiMenu.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>
Global Enum $idOpen = 1000, $idSave, $idInfo

_Main()

Func _Main()
; Create GUI
GUICreate("Menu", 400, 300)
GUICtrlCreateButton("LCDAMVS", 0,0)
GUISetState()
; Register message handlers
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>_Main

; Handle WM_COMMAND messages
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $ilParam
Switch $iwParam
     Case $idOpen
         _WinAPI_ShowMsg("Open")
     Case $idSave
         _WinAPI_ShowMsg("Save")
     Case $idInfo
         _WinAPI_ShowMsg("Info")
EndSwitch
EndFunc ;==>WM_COMMAND

; Handle WM_CONTEXTMENU messages
Func WM_CONTEXTMENU($hWnd, $iMsg, $iwParam, $ilParam)
#forceref $hWnd, $iMsg, $ilParam
Local $hMenu
$hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $idOpen)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $idSave)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $idInfo)
_GUICtrlMenu_TrackPopupMenu($hMenu, $iwParam)
_GUICtrlMenu_DestroyMenu($hMenu)
Return True
EndFunc ;==>WM_CONTEXTMENU
Link to comment
Share on other sites

  • Moderators

TeddyTech,

Your problem arises because you are using $iwParam as the parameter for the _GUICtrlMenu_TrackPopupMenu command. According to MSDN this is "A handle to the window in which the user right-clicked the mouse. This can be a child window of the window receiving the message". So you are asking GUIRegisterMsg to intercept the WM_CONTEXTMENU message for the whole GUI and then passing the button handle to the context menu function when you click over it.

Two solutions:

- 1. Pass the GUI handle to the context menu function - now the menu works everywhere:

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

Global Enum $idOpen = 1000, $idSave, $idInfo
Global $hGUI = 9999

_Main()

Func _Main()
    ; Create GUI
    $hGUI = GUICreate("Menu", 400, 300)
    GUICtrlCreateButton("LCDAMVS", 0, 0)
    GUISetState()
    ; Register message handlers
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>_Main

; Handle WM_COMMAND messages
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $ilParam
    Switch $iwParam
        Case $idOpen
            MsgBox(0, "", "Open")
        Case $idSave
            MsgBox(0, "", "Save")
        Case $idInfo
            MsgBox(0, "", "Info")
    EndSwitch
EndFunc   ;==>WM_COMMAND

; Handle WM_CONTEXTMENU messages
Func WM_CONTEXTMENU($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $ilParam
    Local $hMenu
    $hMenu = _GUICtrlMenu_CreatePopup()
    _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $idOpen)
    _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $idSave)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $idInfo)
    _GUICtrlMenu_TrackPopupMenu($hMenu, $hWnd) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    _GUICtrlMenu_DestroyMenu($hMenu)
    Return True
EndFunc   ;==>WM_CONTEXTMENU

- 2. Check to see if the message is actually sent by clicking on the button - now the menu does not appear:

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

Global Enum $idOpen = 1000, $idSave, $idInfo
Global $hGUI = 9999

_Main()

Func _Main()
    ; Create GUI
    $hGUI = GUICreate("Menu", 400, 300)
    GUICtrlCreateButton("LCDAMVS", 0, 0)
    GUISetState()
    ; Register message handlers
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
    ; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>_Main

; Handle WM_COMMAND messages
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $ilParam
    Switch $iwParam
        Case $idOpen
            MsgBox(0, "", "Open")
        Case $idSave
            MsgBox(0, "", "Save")
        Case $idInfo
            MsgBox(0, "", "Info")
    EndSwitch
EndFunc   ;==>WM_COMMAND

; Handle WM_CONTEXTMENU messages
Func WM_CONTEXTMENU($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $ilParam
    Local $hMenu
    ; Check we are over the GUI and not the button
    If $iwParam = $hGUI Then
        $hMenu = _GUICtrlMenu_CreatePopup()
        _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $idOpen)
        _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $idSave)
        _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
        _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $idInfo)
        _GUICtrlMenu_TrackPopupMenu($hMenu, $iwParam) ; <<<<<<<<<<<<<<<<<<<<<<<
        _GUICtrlMenu_DestroyMenu($hMenu)
        Return True
    EndIf
EndFunc   ;==>WM_CONTEXTMENU

All clear? :)

M23

P.S. The forum software is to blame for the "#include stripping" - but it does not happen if you use the basic editor. ;)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Melba23,

Thanks for the quick ... and perfect ... answer!

I actually played with the parameters on _GUICtrlMenu_TrackPopupMenu() , but I must have made a mistake because it did not resolve my problem.

Thanks again,

TeddyTech

(In case you are interested -- I'm building a toolbar application that allows me to place 1-12 windows on top of each other and toggle through them using buttons on the toolbar. The individual windows are opened on their own and this context menu is going to allow the user to select "Add" to create a button on the toolbar and add the window to the stack.)

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