Jump to content

Keep context menu open after selection


TheSaint
 Share

Recommended Posts

I tried to find some method of keeping the right-click context menu of a button, open, after selecting an entry. There are a few reasons why that might be desirable.
I know I have done that before, possibly with VBA ... or maybe it was with TrayMenu or perhaps older AutoIt.

Anyway, I could not find an existing solution here at the Forum or elsewhere.
However I did come across some code by @LarsJ, that seemed to offer a pathway to a solution.

https://www.autoitscript.com/forum/topic/145091-breaking-main-loop-when-context-menu-is-shown/?do=findComment&comment=1028169

After much experimentation, I eventually came up with a workable solution, that some of you might find useful.
Strictly speaking, it does not keep the context menu open, instead it just instantly re-opens it to the selection (i.e. sub-menu) you just clicked.

Here is my code. Enjoy!

; THANKS to LarsJ for the Menu Style element, that allowed the Case loop to continue while Context Menu is waiting for a selection.
; His example can be found at AutoIt Forums.
; https://www.autoitscript.com/forum/topic/145091-breaking-main-loop-when-context-menu-is-shown/?do=findComment&comment=1028169

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <Misc.au3>

#include <GuiMenu.au3>

Global $Button_menu, $Context_menu, $Extras_menu, $URL_menu
Global $audio_item, $Batch_item, $Close_item, $Exit_item, $HD_item, $Paste_item
Global $audio, $extra, $handle, $HD, $item, $pos, $style, $winpos

$style = $WS_CAPTION + $WS_POPUP + $WS_CLIPSIBLINGS + $WS_SYSMENU
$DropperGUI = GUICreate("Dropbox", 170, 110, Default, Default, $style, $WS_EX_TOPMOST)
$Button_menu = GUICtrlCreateButton("Menu", 126, 85, 40, 19)
GUICtrlSetFont($Button_menu, 7, 600, 0, "Small Fonts")
GUICtrlSetTip($Button_menu, "Program Menu!")
;
; CONTEXT MENU
$Context_menu = GUICtrlCreateContextMenu($Button_menu)
$Paste_item = GUICtrlCreateMenuItem("Paste URL(s)", $Context_menu)
GUICtrlCreateMenuItem("", $Context_menu)
$Batch_item = GUICtrlCreateMenuItem("Batch Mode", $Context_menu, -1, 0)
;
$Extras_menu = GUICtrlCreateMenu("&Extra Options", $Context_menu)
$URL_menu = GUICtrlCreateMenu("&URL Options", $Extras_menu)
$Params_item = GUICtrlCreateMenuItem("Use Extra Parameters", $URL_menu, -1, 0)
$HD_item = GUICtrlCreateMenuItem("HD", $URL_menu, -1, 0)
$audio_item = GUICtrlCreateMenuItem("Convert audio to AAC", $URL_menu, -1, 0)
;
GUICtrlCreateMenuItem("", $Context_menu)
GUICtrlCreateMenuItem("", $Context_menu)
$Close_item = GUICtrlCreateMenuItem("Close", $Context_menu)
GUICtrlCreateMenuItem("", $Context_menu)
$Exit_item = GUICtrlCreateMenuItem("Exit", $Context_menu)

$handle = GUICtrlGetHandle($Context_menu)
_GUICtrlMenu_SetMenuStyle($handle, BitXOR(_GUICtrlMenu_GetMenuStyle($handle), $MNS_MODELESS))

$audio = 4
$extra = 4
$HD = 4

$item = ""
GUISetState(@SW_SHOW)
While 1
    If $item == "extras" Then
        $item = ""
        Send("e")
        Send("u")
        ControlFocus($DropperGUI, "", $handle)
    EndIf
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE Or $msg = $Exit_item
            ; Close the program or Minimize the Window
            If _IsPressed("11") Then
                ; Minimize the Window
                GUISetState(@SW_MINIMIZE, $DropperGUI)
                WinSetTitle($DropperGUI, "", "URL Dropbox was created by TheSaint")
            Else
                ExitLoop
            EndIf
        Case $msg = $GUI_EVENT_MINIMIZE
            WinSetTitle($DropperGUI, "", "URL Dropbox was created by TheSaint")
        Case $msg = $GUI_EVENT_RESTORE
            WinSetTitle($DropperGUI, "", "URL Dropbox")
        Case $msg = $Button_menu
            ; Program Menu
            ControlClick($DropperGUI, "", $Button_menu, "right", 1)
        Case $msg = $Paste_item
            ; Paste URL(s)
        Case $msg = $Params_item
            ; Use Extra Parameters
            If $extra = 1 Then
                $extra = 4
            ElseIf $extra = 4 Then
                $extra = 1
            EndIf
            GUICtrlSetState($Params_item, $extra)
            $item = "extras"
            ReOpenContextMenu()
        Case $msg = $HD_item
            ; HD
            If $HD = 1 Then
                $HD = 4
            ElseIf $HD = 4 Then
                $HD = 1
            EndIf
            GUICtrlSetState($HD_item, $HD)
            $item = "extras"
            ReOpenContextMenu()
        Case $msg = $Batch_item
            ; Batch Mode
        Case $msg = $audio_item
            ; Convert audio to AAC
            If $audio = 1 Then
                $audio = 4
            ElseIf $audio = 4 Then
                $audio = 1
            EndIf
            GUICtrlSetState($audio_item, $audio)
            $item = "extras"
            ReOpenContextMenu()
        Case Else
            ; Other
    EndSelect
WEnd
;
GUISetState(@SW_HIDE)
;
GUIDelete($DropperGUI)

Exit

Func ReOpenContextMenu()
    $mpos = MouseGetPos()
    $winpos = WinGetPos($DropperGUI, "")
    $pos = ControlGetPos($DropperGUI, "", $Button_menu)
    MouseMove($winpos[0] + $pos[0] + 10, $winpos[1] + $pos[1] + 30, 0)
    ControlClick($DropperGUI, "", $Button_menu, "right", 1)
    MouseMove($mpos[0], $mpos[1], 0)
EndFunc

I am fairly certain there is a nicer/cleaner/better method than mine, certainly as far as the SEND commands go, but I could not find it.

The best way to test, is to select one of the sub menu items - Convert audio to AAC

Extra Options -> URL Options -> Convert audio to AAC

It should close and instantly re-open back at that option, with a visible tick now showing.

A perfect example of wanting to keep the context menu open, is when you want to enable all three options in that submenu. Painful to re-browse to set each. You also get instant visual confirmation of the setting.

NOTES

You may note the mouse movement, and wonder why. Firstly, the context menu will always open (top left) at where the mouse cursor last was. So I ensure it is always the same location each time (MENU button). Secondly, after the context menu re-opens, if the mouse cursor is not over a menu entry, then a mouse move closes the sub menus ... defeating the process.

Edited by TheSaint

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

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