Jump to content

Context Menu Expand Direction Upward?


Recommended Posts

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

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

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