Jump to content
nacerbaaziz

how to create a context menu with out autoit GUI

Recommended Posts

hello sirs.
i have a request from you please
i've created a tray Menu tool that have multy items
and i want while the tool is runing if the user open the main exe again a context menu that have the same options that in the tray menu will Appear.

i search on the web about how to do that
i found the _GUICtrlMenu_TrackPopupMenu()
but i realy don't know how to use it
can any one help me to do that please.
here is the example from the help file

#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WinAPIError.au3>
#include <WindowsConstants.au3>
    Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo
    Example()
    Func Example()
    ; Create GUI
    GUICreate("Menu", 400, 300)
    GUISetState(@SW_SHOW)
        ; Register message handlers
    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
    GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")
        ; Loop until the user exits.
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc   ;==>Example
    ; Handle WM_COMMAND messages
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    Switch $wParam
        Case $e_idOpen
            _WinAPI_ShowMsg("Open")
        Case $e_idSave
            _WinAPI_ShowMsg("Save")
        Case $e_idInfo
            _WinAPI_ShowMsg("Info")
    EndSwitch
EndFunc   ;==>WM_COMMAND
    ; Handle WM_CONTEXTMENU messages
Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    Local $hMenu
    $hMenu = _GUICtrlMenu_CreatePopup()
    _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $e_idOpen)
    _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $e_idSave)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $e_idInfo)
    _GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
    _GUICtrlMenu_DestroyMenu($hMenu)
    Return True
EndFunc   ;==>WM_CONTEXTMENU


and here is what i tried

#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WinAPIError.au3>
#include <WindowsConstants.au3>
    Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo
    
    $hMenu = _GUICtrlMenu_CreatePopup()
    _GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $e_idOpen)
    _GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $e_idSave)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
    _GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $e_idInfo)
$hwnd = WinGetHandle("[ACTIVE]", "")
    _GUICtrlMenu_TrackPopupMenu($hMenu, $hwnd)
    _GUICtrlMenu_DestroyMenu($hMenu)
sleep(10000)


but the problem is the menu did not Appeared

Share this post


Link to post
Share on other sites

What you tried won't work very well.  You need to run the TrackPopupMenu func inside of the WM_CONTEXTMENU handler. and you are also destroying the menu right after it is created.  I'm not sure why the example has it coded that way (create and destroy each time), but I was able to tweak the example (below) and it seems to work.

#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WinAPIError.au3>
#include <WindowsConstants.au3>
Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo
Global $hMenu

$hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $e_idOpen)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $e_idSave)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $e_idInfo)

; Create GUI
GUICreate("Menu", 400, 300)
GUISetState(@SW_SHOW)

; Register message handlers
GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")

; Loop until the user exits.
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
_GUICtrlMenu_DestroyMenu($hMenu)

; Handle WM_COMMAND messages
Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    Switch $wParam
        Case $e_idOpen
            _WinAPI_ShowMsg("Open")
        Case $e_idSave
            _WinAPI_ShowMsg("Save")
        Case $e_idInfo
            _WinAPI_ShowMsg("Info")
    EndSwitch
EndFunc   ;==>WM_COMMAND
    ; Handle WM_CONTEXTMENU messages
Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $lParam
    _GUICtrlMenu_TrackPopupMenu($hMenu, $wParam)
    Return True
EndFunc   ;==>WM_CONTEXTMENU

 


Share this post


Link to post
Share on other sites

thx sir

@spudw2k

but not that what i need. i want when the user click on the program the context menu apear automatically. i hope that you can help me to do that

Share this post


Link to post
Share on other sites
On 6/3/2019 at 12:49 AM, spudw2k said:

I'm afraid I don't understand.  Can you explain what you mean more?

hello sir
So I'll try to explain exactly what I want
i've created an tool which work from the system tray
this tool has multi items for example

tool
opened windows
hidden windows
about
exit
and i want to create a pop-up menu  that containe the same items that in the system tray
i meen
tool
opened windows
hidden windows
about
exit

this context menu i want it to apear automaticaly if the user run the tool, and the tool is  already runing
i finished the tool and i want to add this function To facilitate the access to the tool options
because this tool is for the blind users
I hope my idea is clear
and i hope you can help me to do that.
thanks in advance

Share this post


Link to post
Share on other sites

I think I understand.  Upon executing the tool, you want to display the context menu.  Is that correct?

I couldn't find a way to do ti without a GUI Handle to "attach" the context menu to, but you don't need to show a gui.

Here is a small demo you might be able to adapt from.

#include <GUIConstantsEx.au3>
#include <GuiMenu.au3>
#include <WinAPIError.au3>
#include <WindowsConstants.au3>
Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo
Global $hMenu, $hGUI

$hGUI = GUICreate("",0,0)

$hMenu = _GUICtrlMenu_CreatePopup()
_GUICtrlMenu_InsertMenuItem($hMenu, 0, "Open", $e_idOpen)
_GUICtrlMenu_InsertMenuItem($hMenu, 1, "Save", $e_idSave)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "", 0)
_GUICtrlMenu_InsertMenuItem($hMenu, 3, "Info", $e_idInfo)

; Register message handlers
;GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
;GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU")

$iContextMenuItem = _GUICtrlMenu_TrackPopupMenu($hMenu, $hGUI, @DesktopWidth/2, @DesktopHeight/2, 0, 0, 2)
msgbox(0,"Selected Context Menu Item Selected", $iContextMenuItem)

_GUICtrlMenu_DestroyMenu($hMenu)

Play around with it and let us know where you get stuck or what questions you have.


Share this post


Link to post
Share on other sites

good morning

thank you all to your help

now i succeded  and i did what i was looking for

thank you very much to all of you

please accept my greetings

 

Share this post


Link to post
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

×
×
  • Create New...