Jump to content
Sign in to follow this  

example script in helpfile

Recommended Posts


take a look at this script from the helpfile:

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

Opt('MustDeclareVars', 1)


; ****************
; * First sample *
; ****************
Func Example1()
    Local $contextmenu, $button, $buttoncontext, $buttonitem, $msg
    Local $newsubmenu, $textitem, $fileitem, $saveitem, $infoitem

    ;right click on gui to bring up context Menu.
    ;right click on the "ok" button to bring up a controll specific context menu.

    GUICreate("My GUI Context Menu", 300, 200)

    $contextmenu = GUICtrlCreateContextMenu()

    $button = GUICtrlCreateButton("OK", 100, 100, 70, 20)
    $buttoncontext = GUICtrlCreateContextMenu($button)
    $buttonitem = GUICtrlCreateMenuItem("About button", $buttoncontext)

    $newsubmenu = GUICtrlCreateMenu("new", $contextmenu)
    $textitem = GUICtrlCreateMenuItem("text", $newsubmenu)

    $fileitem = GUICtrlCreateMenuItem("Open", $contextmenu)
    $saveitem = GUICtrlCreateMenuItem("Save", $contextmenu)
    GUICtrlCreateMenuItem("", $contextmenu)     ; separator

    $infoitem = GUICtrlCreateMenuItem("Info", $contextmenu)


    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
EndFunc   ;==>Example1

; *****************
; * Second sample *
; *****************
Func Example2()
    Local $hGui, $OptionsBtn, $OptionsDummy, $OptionsContext, $OptionsCommon, $OptionsFile, $msg
    Local $OptionsExit, $HelpBtn, $HelpDummy, $HelpContext, $HelpWWW, $HelpAbout
    $hGui = GUICreate("My GUI", 170, 40)

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

    ; At first create a dummy control for the options and a contextmenu for it
    $OptionsDummy = GUICtrlCreateDummy()
    $OptionsContext = GUICtrlCreateContextMenu($OptionsDummy)
    $OptionsCommon = GUICtrlCreateMenuItem("Common", $OptionsContext)
    $OptionsFile = GUICtrlCreateMenuItem("File", $OptionsContext)
    GUICtrlCreateMenuItem("", $OptionsContext)
    $OptionsExit = GUICtrlCreateMenuItem("Exit", $OptionsContext)

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

    ; Create a dummy control and a contextmenu for the help too
    $HelpDummy = GUICtrlCreateDummy()
    $HelpContext = GUICtrlCreateContextMenu($HelpDummy)
    $HelpWWW = GUICtrlCreateMenuItem("Website", $HelpContext)
    GUICtrlCreateMenuItem("", $HelpContext)
    $HelpAbout = GUICtrlCreateMenuItem("About...", $HelpContext)


    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $OptionsExit, $GUI_EVENT_CLOSE
            Case $OptionsBtn
                ShowMenu($hGui, $msg, $OptionsContext)
            Case $HelpBtn
                ShowMenu($hGui, $msg, $HelpContext)
            Case $HelpAbout
                MsgBox(64, "About...", "GUICtrlGetHandle-Sample")
EndFunc   ;==>Example2

; Show a menu in a given GUI window which belongs to a given GUI ctrl
Func ShowMenu($hWnd, $CtrlID, $nContextID)
    Local $arPos, $x, $y
    Local $hMenu = GUICtrlGetHandle($nContextID)
    $arPos = ControlGetPos($hWnd, "", $CtrlID)
    $x = $arPos[0]
    $y = $arPos[1] + $arPos[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 $stPoint = DllStructCreate("int;int")
    DllStructSetData($stPoint, 1, $x)
    DllStructSetData($stPoint, 2, $y)

    DllCall("user32.dll", "int", "ClientToScreen", "hwnd", $hWnd, "ptr", DllStructGetPtr($stPoint))
    $x = DllStructGetData($stPoint, 1)
    $y = DllStructGetData($stPoint, 2)
    ; release Struct not really needed as it is a local
    $stPoint = 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

If you run it you'll see when you left click on the options button a context meny pops up right under it. Well I added all the necessary functions from this script into my own and didn't changed any variables and the context menu is coming up WAY at the bottom of my script.

I guess my question would be do I have to changed the way it assigns the coords? Because at the bottom of my window its approximately 5 pixels from the bottom and the context menu in this script is approximately 5 pixels from the bottom.

Share this post

Link to post
Share on other sites

How do you expect us to tell you what you did wrong when you don't post your code?? :P

Post a short working reproducer that shows the symptoms or you will only get answers like:

"you have to many cats in your trousers, insert an alligator and your script will work lolzors!!!11!!1one!!"

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
Sign in to follow this