Jump to content

[Solved] How to dismiss right click on context menu?


qwert
 Share

Recommended Posts

This is fairly basic case and question—and there's probably a better title for the post—but I can't recall ever encountering this situation:

What's the proper way to dismiss the right click mouse action for a context menu so that it doesn't invoke other processing?

I've worked up a small script of what I'm observing.  A right click on the background displays a message.  Everything works as expected except that the right click on the center label should only invoke the context menu and do nothing else.

Thanks in advance for any help.

 

#include <GUIConstantsEx.au3>

$GUI = GUICreate("Test panel", 300, 200)
GUICtrlCreateLabel("Right click on background for message", 40, 120, 200, 24)

$Label = GUICtrlCreateLabel("Control", 60, 40, 160, 24, 0x01) ; $SS_CENTER
GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
GUICtrlSetBkColor(-1, 0xDDFFDD)
GUICtrlSetTip(-1, "Right click for menu")

$idMenu = GUICtrlCreateContextMenu($Label) ;GUICtrlCreateDummy()
$idChoice1 = GUICtrlCreateMenuItem("Menu Choice 1", $idMenu)
$idChoice2 = GUICtrlCreateMenuItem("Menu Choice 2", $idMenu)

GUISetState()

; script must run in message loop mode:
While 1
    $aMsg = GUIGetMsg(1)            ; get additional information
    Switch $aMsg[0]
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idChoice1
            MsgBox(0, "Left click", "Choice 1")
        Case $idChoice2
            MsgBox(0, "Left click", "Choice 2")
        Case $GUI_EVENT_SECONDARYUP
            MsgBox(0, "Right click", " ... on background")
    EndSwitch
WEnd

 

Edited by qwert
Link to comment
Share on other sites

Technically the "Right click on background" isn't a context menu, it's just a MsgBox

If you want to have a proper context menu you can just create another Context menu for example:

#include <GUIConstantsEx.au3>

$GUI = GUICreate("Test panel", 300, 200)
$idBackground = GUICtrlCreateContextMenu(-1)
$idChoice1 = GUICtrlCreateMenuItem("Right Click on Background", $idBackground)

GUICtrlCreateLabel("Right click on background for message", 40, 120, 200, 24)

$Label = GUICtrlCreateLabel("Control", 60, 40, 160, 24, 0x01) ; $SS_CENTER
GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
GUICtrlSetBkColor(-1, 0xDDFFDD)
GUICtrlSetTip(-1, "Right click for menu")

$idMenu = GUICtrlCreateContextMenu($Label) ;GUICtrlCreateDummy()
$idChoice1 = GUICtrlCreateMenuItem("Menu Choice 1", $idMenu)
$idChoice2 = GUICtrlCreateMenuItem("Menu Choice 2", $idMenu)

GUISetState()

; script must run in message loop mode:
While 1
    $aMsg = GUIGetMsg(1)            ; get additional information
    Switch $aMsg[0]
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idChoice1
            MsgBox(0, "Left click", "Choice 1")
        Case $idChoice2
            MsgBox(0, "Left click", "Choice 2")
    EndSwitch
WEnd

 

Link to comment
Share on other sites

Subz, thanks for the suggestion.  Indeed, with only a slight enhancement, it solves the case I'm working on at the moment.

 

#include <GUIConstantsEx.au3>

$GUI = GUICreate("Test panel", 300, 200)
$idBackground = GUICtrlCreateContextMenu(-1)
$idChoiceBG = GUICtrlCreateMenuItem("Right Click on Background", $idBackground)

GUICtrlCreateLabel("Right click on background for message", 40, 120, 200, 24)

$Label = GUICtrlCreateLabel("Control", 60, 40, 160, 24, 0x01) ; $SS_CENTER
GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
GUICtrlSetBkColor(-1, 0xDDFFDD)
GUICtrlSetTip(-1, "Right click for menu")

$idMenu = GUICtrlCreateContextMenu($Label) ;GUICtrlCreateDummy()
$idChoice1 = GUICtrlCreateMenuItem("Menu Choice 1", $idMenu)
$idChoice2 = GUICtrlCreateMenuItem("Menu Choice 2", $idMenu)

GUISetState()

; script must run in message loop mode:
While 1
    $aMsg = GUIGetMsg(1)            ; get additional information
    Switch $aMsg[0]
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idChoice1
            MsgBox(0, "Left click", "Choice 1")
        Case $idChoice2
            MsgBox(0, "Left click", "Choice 2")
        Case $idChoiceBG
            MsgBox(0, "Left click", "Choice BG")
    EndSwitch
WEnd

But if anyone knows a way to actually dismiss the event in my first script, it would come in handy for other cases.  And if it's just not possible, I'd like to know that, as well.

I appreciate the help.

Link to comment
Share on other sites

One way:

#include <GUIConstantsEx.au3>

Opt("MouseCoordMode", 2)

$GUI = GUICreate("Test panel", 300, 200)
GUICtrlCreateLabel("Right click on background for message", 40, 120, 200, 24)

$Label = GUICtrlCreateLabel("Control", 60, 40, 160, 24, 0x01) ; $SS_CENTER
GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
GUICtrlSetBkColor(-1, 0xDDFFDD)
GUICtrlSetTip(-1, "Right click for menu")

$idMenu = GUICtrlCreateContextMenu($Label) ;GUICtrlCreateDummy()
$idChoice1 = GUICtrlCreateMenuItem("Menu Choice 1", $idMenu)
$idChoice2 = GUICtrlCreateMenuItem("Menu Choice 2", $idMenu)

GUISetState()

; script must run in message loop mode:
While 1
    $aMsg = GUIGetMsg(1)            ; get additional information
    Switch $aMsg[0]
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idChoice1
            MsgBox(0, "Left click", "Choice 1")
        Case $idChoice2
            MsgBox(0, "Left click", "Choice 2")
        Case $GUI_EVENT_SECONDARYUP
            $aControlPos = ControlGetPos($GUI, "", $Label)
            $aMousePos = MouseGetPos()
            If $aMousePos[0] >= $aControlPos[0] And $aMousePos[0] <= $aControlPos[0] + $aControlPos[2] And $aMousePos[1] >= $aControlPos[1] And $aMousePos[1] <= $aControlPos[1] + $aControlPos[3] Then ContinueLoop
            MsgBox(0, "Right click", " ... on background")
    EndSwitch
WEnd

 

Link to comment
Share on other sites

Another way:

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

$GUI = GUICreate("Test panel", 300, 200)
GUICtrlCreateLabel("Right click on background for message", 40, 120, 200, 24)

$Label = GUICtrlCreateLabel("Control", 60, 40, 160, 24, 0x01) ; $SS_CENTER
GUICtrlSetFont(-1, 14, 400, 0, "Tahoma")
GUICtrlSetBkColor(-1, 0xDDFFDD)
GUICtrlSetTip(-1, "Right click for menu")

$idMenu = GUICtrlCreateContextMenu(GUICtrlCreateDummy())
$idChoice1 = GUICtrlCreateMenuItem("Menu Choice 1", $idMenu)
$idChoice2 = GUICtrlCreateMenuItem("Menu Choice 2", $idMenu)

GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idChoice1
            MsgBox(0, "Left click", "Choice 1")
        Case $idChoice2
            MsgBox(0, "Left click", "Choice 2")
        Case $GUI_EVENT_SECONDARYUP
            Switch GUIGetCursorInfo($GUI)[4]
                Case $Label
                    _GUICtrlMenu_TrackPopupMenu(GUICtrlGetHandle($idMenu), $GUI)
                Case Else
                    MsgBox(0, "Right click", " ... on background")
            EndSwitch
    EndSwitch
WEnd

 

Link to comment
Share on other sites

@InnI, I like that method.  Thanks.  For anyone following, it creates the context menu, but waits for a right mouse click to determine if it should be displayed.  All other right clicks get the background message. 

@Subz, that's a close second ... except that where the mouse click actually occurs on the GUI affects the response.  (as shown below)

Thanks for both suggestions.  I consider this "solved".

5b1f11b289629_Backgroundclick.png.f0788f1deb3def7d8c32a22b66de4277.png

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