Jump to content

Create a context menu without a right click


Go to solution Solved by ioa747,

Recommended Posts

Hello,

I would like a context menu to pop up at my mouse when I press a hotkey like F3.  Then I would select something within that menu to do additional actions.  I really like the look of a context menu instead of making a GUI.  Everything I've seen so far, like in the examples, is right clicking on a custom GUI that allows a custom context menu.  I just want to be able to trigger this context menu with a hotkey no matter what program or window is active.  If this is not possible, then I guess I'd have to create a gui to mimic how a context menu looks.  Just hoping there was a better way.  Thanks!

Link to comment
Share on other sites

  • Solution

for the example I took a piece from
https://www.autoitscript.com/forum/topic/208404-scite-plusbar/?do=findComment&comment=1513490

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

Global $MyIni = StringTrimRight(@ScriptFullPath, 4) & ".ini"
Global $MyName = StringTrimRight(@ScriptName, 4)
ConsoleWrite("$MyName=" & $MyName & @CRLF)

HotKeySet("{F3}", "_MyConex")

;**********************************
While 1
    Sleep(50)
WEnd
;**********************************

Func _MyConex()
    Global $mPos = MouseGetPos()
    Global $hGui = GUICreate("Fav_gui", 32, 32, $mPos[0], $mPos[1], $WS_POPUP, BitOR($WS_EX_TOOLWINDOW, $WS_EX_TOPMOST, $WS_EX_NOACTIVATE))
    GUISetBkColor("0x000000")
    WinSetTrans($hGui, "", 2)

    ;Add FileChangeDir to set the workdir to the scriptdir
    FileChangeDir(@ScriptDir)

;~ Global $aBox = BoxLoad()

    #Region === ContextMenu ===
    Global $hMenu = GUICtrlCreateContextMenu()

    Global $Menu_Add = GUICtrlCreateMenuItem("➕  Add To " & $MyName, $hMenu)
    Global $Menu_Remove = GUICtrlCreateMenuItem("➖  Remove From " & $MyName, $hMenu)
    GUICtrlCreateMenuItem("", $hMenu) ; separator   -------------------------------------
    Global $Menu_Edit = GUICtrlCreateMenuItem("⚙  Edit " & $MyName, $hMenu)
    GUICtrlCreateMenuItem("", $hMenu) ; separator   -------------------------------------

;~ For $i = 1 To $aBox[0][0]
;~     GUICtrlCreateMenuItem($aBox[$i][0], $hMenu)
;~ Next

    GUICtrlCreateMenuItem("", $hMenu) ; separator   -------------------------------------
    Global $Menu_Cancel = GUICtrlCreateMenuItem("Cancel", $hMenu)
    #EndRegion === ContextMenu ===
    GUISetState(@SW_SHOW)

    ; Perform a mouse right click
    MouseClick("right")

    Global $ID, $TmpSciteFile, $NeedSave = False
    ; Loop until the user exits.
    ;********************************************************************
    While 1
        $ID = GUIGetMsg()
        Switch $ID
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $Menu_Add
                ;...
                ExitLoop
            Case $Menu_Remove
                ;...
                ExitLoop
            Case $Menu_Edit
                ;...
                ExitLoop
            Case $Menu_Cancel
                ExitLoop
;~         Case 8 To $aBox[0][0] + 9
;~             ExitLoop
        EndSwitch

        If Not WinActive($hGui) Then
            ExitLoop
        EndIf

    WEnd
    ;********************************************************************
EndFunc   ;==>_MyConex

 

I know that I know nothing

Link to comment
Share on other sites

@ioa747  Thank you very much for this.  It does everything I wanted.  I was just expecting how to initiate the context menu, but you have my hotkey and everything.  Reading through this, It looks like you basically create a transparent gui that you right click on which initiates the context menu.  Clever!  

Link to comment
Share on other sites

Another approach :

#include <GUIConstants.au3>
#include <GuiMenu.au3>

HotKeySet("{F3}", ShowMenu)
HotKeySet("^{F3}", Terminate)

While Sleep(100)
WEnd

Func ShowMenu()
  Local Static $bCreated = False
  Local Static $hGUI = GUICreate("", 50, 50, -100, -100, $WS_POPUP, $WS_EX_TOOLWINDOW)

  Local Static $idContextmenu = GUICtrlCreateContextMenu()
  Local Static $idNew = GUICtrlCreateMenuItem("New", $idContextmenu)
  Local Static $idOpen = GUICtrlCreateMenuItem("Open", $idContextmenu)
  Local Static $idSave = GUICtrlCreateMenuItem("Save", $idContextmenu)
  If Not $bCreated Then GUICtrlCreateMenuItem("", $idContextmenu)   ; separator
  Local Static $idInfo = GUICtrlCreateMenuItem("Info", $idContextmenu)
  Local Static $hMenu = GUICtrlGetHandle($idContextmenu)

  $bCreated = True

  GUISetState()

  Switch _GUICtrlMenu_TrackPopupMenu($hMenu, $hGUI, -1, -1, 1, 1, 2)
    Case $idNew
      ConsoleWrite("New" & @CRLF)
    Case $idOpen
      ConsoleWrite("Open" & @CRLF)
    Case $idSave
      ConsoleWrite("Save" & @CRLF)
    Case $idInfo
      ConsoleWrite("Info" & @CRLF)
  EndSwitch
  GUISetState(@SW_HIDE)
EndFunc   ;==>ShowMenu

Func Terminate()
  Exit
EndFunc   ;==>Terminate

 

Edited by Nine
creation only once
Link to comment
Share on other sites

You can compare code by yourself.  But to help you understand, I use _GUICtrlMenu_TrackPopupMenu to show the context menu instead of using a right-click.  Which in my perspective is way more robust and much cleaner (no offense to @ioa747

ps. also by using Static controls, you do not need to create the GUI each time you press F3.  It is created once, and you reuse it as often as you want.

Edited by Nine
Link to comment
Share on other sites

@Nine  Thank you for the answer.  I looked through the code and I saw you did things slightly different and I was wondering the "why" from your perspective.  You answered my question.  That's interesting on the "Static" controls.  I did not know that was a thing.  Thank you.

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