DJSizzlePuff

Context Menu Expand Direction Upward?

3 posts in this topic

Hi all! I'm working on my first GUI application, and there's something I'm trying to figure out how to do. I've searched the forums and the help file and didn't see anything that could help me.

I have a context menu that opens from a button on the bottom of my GUI's form. What I'd *like* it to do is have it expand upward instead of downward. I'm using Example 2 from the help file section on GUICtrlCreateContextMenu as the basis for what I'm doing.

My question is: is there something simple like a flag I can set or some other trick to just tell the context menu which way to expand, something like "ExpandDirection"? The other way I guess would be to figure out the height of the context menu and subtract that rather than add that from the position of my button control, however I tried using ControlGetPos on the controlID of the GUICtrlCreateContextMenu variable (as well as the GUICtrlCreateDummy variable) and that doesn't seem to work.

Any advice on how to get a context menu to open the way I want it to?

Also, what's the best way to set the width of a context menu despite how long the actual subitems in the context menu are? I know I can pad the menu item text with extra spaces, but is there a way to set exact pixels instead?

(By the way, I've attached a screen shot of what I've got and what I'd like to accomplish, as well as the code I'm using from Example 2 of GUICtrlCreateContextMenu)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $hGui = GUICreate("My GUI", 170, 40)

    Local $idOptionsBtn = GUICtrlCreateButton("&Options", 10, 10, 70, 20, $BS_FLAT)

    ; At first create a dummy control for the options and a contextmenu for it
    Local $idOptionsDummy = GUICtrlCreateDummy()
    Local $idOptionsContext = GUICtrlCreateContextMenu($idOptionsDummy)
    GUICtrlCreateMenuItem("Common", $idOptionsContext)
    GUICtrlCreateMenuItem("File", $idOptionsContext)
    GUICtrlCreateMenuItem("", $idOptionsContext)
    Local $idOptionsExit = GUICtrlCreateMenuItem("Exit", $idOptionsContext)

    Local $idHelpBtn = GUICtrlCreateButton("&Help", 90, 10, 70, 20, $BS_FLAT)

    ; Create a dummy control and a contextmenu for the help too
    Local $idHelpDummy = GUICtrlCreateDummy()
    Local $idHelpContext = GUICtrlCreateContextMenu($idHelpDummy)
    GUICtrlCreateMenuItem("Website", $idHelpContext)
    GUICtrlCreateMenuItem("", $idHelpContext)
    Local $idHelpAbout = GUICtrlCreateMenuItem("About...", $idHelpContext)

    GUISetState(@SW_SHOW)

    Local $idMsg

    ; Loop until the user exits.
    While 1
        $idMsg = GUIGetMsg()

        Switch $idMsg
            Case $idOptionsExit, $GUI_EVENT_CLOSE
                ExitLoop

            Case $idOptionsBtn
                ShowMenu($hGui, $idMsg, $idOptionsContext)

            Case $idHelpBtn
                ShowMenu($hGui, $idMsg, $idHelpContext)

            Case $idHelpAbout
                MsgBox($MB_SYSTEMMODAL, "About...", "GUICtrlGetHandle-Sample")
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>Example

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $idCtrl, $idContext)
    Local $aPos, $x, $y
    Local $hMenu = GUICtrlGetHandle($idContext)

    $aPos = ControlGetPos($hWnd, "", $idCtrl)

    $x = $aPos[0]
    $y = $aPos[1] + $aPos[3]

    ClientToScreen($hWnd, $x, $y)
    TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc   ;==>ShowMenu

; Convert the client (GUI) coordinates to screen (desktop) coordinates
Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
    Local $tPoint = DllStructCreate("int;int")

    DllStructSetData($tPoint, 1, $x)
    DllStructSetData($tPoint, 2, $y)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($tPoint))

    $x = DllStructGetData($tPoint, 1)
    $y = DllStructGetData($tPoint, 2)
    ; release Struct not really needed as it is a local
    $tPoint = 0
EndFunc   ;==>ClientToScreen

; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0, "int", $x, "int", $y, "hwnd", $hWnd, "ptr", 0)
EndFunc   ;==>TrackPopupMenu

 

context menu expand direction.png

Share this post


Link to post
Share on other sites



Look into the flag of TrackPopupMenuEx. 

An Example:

 

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $hGui = GUICreate("My GUI", 170, 40)

    Local $idOptionsBtn = GUICtrlCreateButton("&Options", 10, 10, 70, 20, $BS_FLAT)

    ; At first create a dummy control for the options and a contextmenu for it
    Local $idOptionsDummy = GUICtrlCreateDummy()
    Local $idOptionsContext = GUICtrlCreateContextMenu($idOptionsDummy)
    GUICtrlCreateMenuItem("Common", $idOptionsContext)
    GUICtrlCreateMenuItem("File", $idOptionsContext)
    GUICtrlCreateMenuItem("", $idOptionsContext)
    Local $idOptionsExit = GUICtrlCreateMenuItem("Exit", $idOptionsContext)

    Local $idHelpBtn = GUICtrlCreateButton("&Help", 90, 10, 70, 20, $BS_FLAT)

    ; Create a dummy control and a contextmenu for the help too
    Local $idHelpDummy = GUICtrlCreateDummy()
    Local $idHelpContext = GUICtrlCreateContextMenu($idHelpDummy)
    GUICtrlCreateMenuItem("Website", $idHelpContext)
    GUICtrlCreateMenuItem("", $idHelpContext)
    Local $idHelpAbout = GUICtrlCreateMenuItem("About...", $idHelpContext)

    GUISetState(@SW_SHOW)

    Local $idMsg

    ; Loop until the user exits.
    While 1
        $idMsg = GUIGetMsg()

        Switch $idMsg
            Case $idOptionsExit, $GUI_EVENT_CLOSE
                ExitLoop

            Case $idOptionsBtn
                ShowMenu($hGui, $idMsg, $idOptionsContext)

            Case $idHelpBtn
                ShowMenu($hGui, $idMsg, $idHelpContext)

            Case $idHelpAbout
                MsgBox($MB_SYSTEMMODAL, "About...", "GUICtrlGetHandle-Sample")
        EndSwitch
    WEnd
    GUIDelete()
EndFunc   ;==>Example

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $idCtrl, $idContext)
    Local $aPos, $x, $y
    Local $hMenu = GUICtrlGetHandle($idContext)

    $aPos = ControlGetPos($hWnd, "", $idCtrl)

    $x = $aPos[0]
    $y = $aPos[1] + $aPos[3]

    ClientToScreen($hWnd, $x, $y)
    TrackPopupMenu($hWnd, $hMenu, $x, $y)
EndFunc   ;==>ShowMenu

; Convert the client (GUI) coordinates to screen (desktop) coordinates
Func ClientToScreen($hWnd, ByRef $x, ByRef $y)
    Local $tPoint = DllStructCreate("int;int")

    DllStructSetData($tPoint, 1, $x)
    DllStructSetData($tPoint, 2, $y)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($tPoint))

    $x = DllStructGetData($tPoint, 1)
    $y = DllStructGetData($tPoint, 2)
    ; release Struct not really needed as it is a local
    $tPoint = 0
EndFunc   ;==>ClientToScreen

; Show at the given coordinates (x, y) the popup menu (hMenu) which belongs to a given GUI window (hWnd)
Func TrackPopupMenu($hWnd, $hMenu, $x, $y)
    DllCall("user32.dll", "int", "TrackPopupMenuEx", "hwnd", $hMenu, "int", 0x0020, "int", $x, "int", $y-20, "hwnd", $hWnd, "ptr", 0)
EndFunc   ;==>TrackPopupMenu

Saludos

Share this post


Link to post
Share on other sites

Oh sweet! So simple. Thank you!

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

  • Similar Content

    • ChrisL
      By ChrisL
      #include <GUIConstantsEx.au3> #include <GuiTreeView.au3> #include <WindowsConstants.au3> #include <GuiMenu.au3> Global $g_hTreeView Global Enum $e_idOpen = 1000, $e_idSave, $e_idInfo Example() Func Example() Local $hGUI, $hItem Local $iStyle = BitOR($TVS_EDITLABELS, $TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS, $TVS_CHECKBOXES) $hGUI = GUICreate("(UDF Created) TreeView Create", 400, 300) $g_hTreeView = _GUICtrlTreeView_Create($hGUI, 2, 2, 396, 268, $iStyle, $WS_EX_CLIENTEDGE) GUISetState(@SW_SHOW) GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") GUIRegisterMsg($WM_CONTEXTMENU, "WM_CONTEXTMENU") GUIRegisterMsg($WM_COMMAND, "WM_COMMAND") _GUICtrlTreeView_BeginUpdate($g_hTreeView) For $x = 1 To Random(2, 10, 1) $hItem = _GUICtrlTreeView_Add($g_hTreeView, 0, StringFormat("[%02d] New Item", $x)) For $y = 1 To Random(2, 10, 1) _GUICtrlTreeView_AddChild($g_hTreeView, $hItem, StringFormat("[%02d] New Child", $y)) Next Next _GUICtrlTreeView_EndUpdate($g_hTreeView) ; Loop until the user exits. Do Until GUIGetMsg() = $GUI_EVENT_CLOSE GUIDelete() EndFunc ;==>Example Func WM_CONTEXTMENU($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $lParam ;ConsoleWrite($GUI_RUNDEFMSG & @CRLF) ;Switch $wParam ; Case $hTreeView 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 ;EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_CONTEXTMENU Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $lParam Switch $wParam Case $e_idOpen _DebugPrint("WM_COMMAND " & $wParam & " Open") Case $e_idSave _DebugPrint("WM_COMMAND " & $wParam & " Save") Case $e_idInfo _DebugPrint("WM_COMMAND " & $wParam & " Info") EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_COMMAND Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam) #forceref $hWnd, $iMsg, $wParam Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndTreeview $hWndTreeview = $g_hTreeView If Not IsHWnd($g_hTreeView) Then $hWndTreeview = GUICtrlGetHandle($g_hTreeView) $tNMHDR = DllStructCreate($tagNMHDR, $lParam) $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom")) $iIDFrom = DllStructGetData($tNMHDR, "IDFrom") $iCode = DllStructGetData($tNMHDR, "Code") Switch $hWndFrom Case $hWndTreeview Switch $iCode Case $NM_CLICK ; The user has clicked the left mouse button within the control _DebugPrint("$NM_CLICK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _ "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _ "-->Code:" & @TAB & $iCode) ; Return 1 ; nonzero to not allow the default processing Return 0 ; zero to allow the default processing Case $NM_DBLCLK ; The user has double-clicked the left mouse button within the control _DebugPrint("$NM_DBLCLK" & @CRLF & "--> hWndFrom:" & @TAB & $hWndFrom & @CRLF & _ "-->IDFrom:" & @TAB & $iIDFrom & @CRLF & _ "-->Code:" & @TAB & $iCode) ; Return 1 ; nonzero to not allow the default processing Return 0 ; zero to allow the default processing EndSwitch EndSwitch Return $GUI_RUNDEFMSG EndFunc ;==>WM_NOTIFY Func _DebugPrint($s_Text, $sLine = @ScriptLineNumber) ConsoleWrite( _ "!===========================================================" & @CRLF & _ "+======================================================" & @CRLF & _ "-->Line(" & StringFormat("%04d", $sLine) & "):" & @TAB & $s_Text & @CRLF & _ "+======================================================" & @CRLF) EndFunc ;==>_DebugPrint In the example code I have a treeview and a context/popup menu, I can't use a conventional context menu because I need to use the treeview UDF functions.
      The WM_COMMAND never fires from the context/popup menu selection while there is a treeview using  _GuiCtrlTreeview_Create
      If you comment out _GuiCtrlTreeview_Create line you will then see the right click selection works as expected and the debug is written to the console on selection.
      Autoit Ver 3.3.14.2
      Any ideas why this combination won't work together?
      Thanks
       
    • dubd83
      By dubd83
      I am working on an automation project and I am having issues finding documentation on selecting an item from a context menu, or a menu that appears upon a right click. I tried using the Send("{DOWN}") command as a workaround but it's not very redundant. Essentially I need to search the context menu for the string "Abort Text" and click that option. The class of the context menu is showing as CLASS:#32768. I have uploaded a portion of the context menu. If it helps I have the handle of the treeview where the first item is right clicked in order to make the treeview appear.
       
       

    • Leo1906
      By Leo1906
      Hello Guys,
      I was looking for a way to disable a Windows Context Menu Entry (not delete it!). So that it's not clickable and greyed out, like the "Empty Recycle Bin" Entry when the Recycle Bin is empty ..
      I did a lot of reseach, but all I could find was this one: 
       But it doesn't work for me ..
      I also took a look at this one: 
      But I wasn't able to properly run it on Windows 10 x64. So it was no help either ..
      I need a command to simply disable and enable the Entry. :/
      Any ideas on this one?
    • Stacker
      By Stacker
      Hi all,
      i need to enable/disable an item($listitem3) on context menu if find a specific value in listview ($idlistview).The item start disable (see code)
      I use GUIGetMsg() in my code
       
      $listtxt = GUICtrlCreateContextMenu($idListView) $listitem1 = GUICtrlCreateMenuItem("Ignore", $idListView) $listitem2 = GUICtrlCreateMenuItem("Remove Ignore", $idListView) $listitem3 = GUICtrlCreateMenuItem("Provide", $idListView) GUICtrlSetState(-1,$GUI_disable) Any suggestion ?
      Thanks
       
       
       
       
    • ur
      By ur
      I wrote below code to delete a file.
      But when I am trying to delete a shortcut, it it deleting target file instead of shortcut when I created a context menu entry for this executable.
      The executable is working fine if we use it normally instead of context menu.But with COntext menu it is taking the target instead of shortcut.
       
      By using the below registries.
      Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\Delete Permanently\command] @="\"C:\\Program Files\\Permanent Delete\\Delete Permanently.exe\" \"%1\"" And compiled the below code to executable.
       
      DeleteFile($CmdLine[$i]) ; Adding the path through CMD line argument. Func DeleteFile($sFilePath) Local $iDelete = FileDelete($sFilePath) ; Display a message of whether the file was deleted. If $iDelete Then MsgBox($MB_ICONINFORMATION, "Deleting ...", "The file was successfuly deleted: " & $sFilePath,10) Else MsgBox($MB_ICONERROR, "Deleting ...", "An error occurred while deleting the file: " & $sFilePath & @CRLF & "Please check whether it is in use by any process",10) EndIf EndFunc