Jump to content

Help with _GUICtrlMenu_InsertMenuItem and GUIOnEventMode


Go to solution Solved by Danyfirex,

Recommended Posts

Posted

Good afternoon all,

I'm having difficulty with a script I'm working on... for years I've always used GUIOnEventMode and its never been an issue; this time I thought I'd include a menu. However this is where I seem to have run into a brick wall. When I select either the Connect or Exit options via the menu the script executes the _Connect() function. I've searched the examples and parts of the forum but I'm struggling to find a solution.

Sample code below:

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

Opt('GUIOnEventMode' , 1)

Global $sTitle = "Test Application"                                     ; Set GUI Title
Global $iWidth = 420, $iHeight = 420                                    ; Set GUI Dimensions

Global $iLabelColumn = 20, $iInputColumn = 100, $iButtonColumn = 340    ; Set Columns

Global $iRow = 30, $iLabelRow = $iRow + 3

Global $iLabelWidth = 75, $iLabelHeight = 20, $iLabelSpace = 10         ; Set Label Dimensions
Global $iInputWidth = 230, $iInputHeight = 20, $iInputSpace = 10        ; Set Input Dimensions
Global $iButtonWidth = 60, $iButtonHeight = 20, $iButtonSpace = 10      ; Set Button Dimensions

Global $hGUI = GUICreate($sTitle, $iWidth, $iHeight)                    ; Create GUI
Global $hFileMenu, $idConnect, $idExit
Global $g_iStyle = $MNS_CHECKORBMP ; default mode
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")                                ; Run this function when the main GUI [X] is clicked

_BuildGUI()

; Loop until the user exits.
    While 1
        Sleep(250)
    WEnd
_Quit()

Func _CreateMenu()
; Create File menu
    $hFileMenu = _GUICtrlMenu_CreateMenu()
    _GUICtrlMenu_InsertMenuItem($hFileMenu, 0, "&Connect", $idConnect)
    _GUICtrlMenu_InsertMenuItem($hFileMenu, 1, "", 0)
    _GUICtrlMenu_InsertMenuItem($hFileMenu, 2, "E&xit", $idExit)

; Create Main menu
    $hMenu = _GUICtrlMenu_CreateMenu($g_iStyle) ; ..for MNS_MODELESS, only this "main menu" is needed.
    _GUICtrlMenu_InsertMenuItem($hMenu, 0, "&File", 0, $hFileMenu)

; Set window menu
    _GUICtrlMenu_SetMenu($hGUI, $hMenu)
EndFunc

Func _BuildGUI()
    _CreateMenu()

    $iLine = 0
    GUICtrlCreateLabel("Status: ", $iLabelColumn, $iLabelRow + $iLine * $iLabelHeight + $iLine * $iLabelSpace, $iLabelWidth, $iLabelHeight)
    $inpCtrl = GUICtrlCreateInput("", $iInputColumn, $iRow + $iLine * $iInputHeight + $iLine * $iInputSpace, $iInputWidth, $iInputHeight)
    GUISetState(@SW_SHOW, $hGUI)                    ; Display the GUI

    GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")
EndFunc

Func _Connect()
    Msgbox(4096, $sTitle, "Do something...", 10)
    Return
EndFunc

; Handle menu commands
Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)
    Switch _WinAPI_LoWord ($iwParam)
        Case $idConnect
            MsgBox(4096, $sTitle, "Connect was selected")
            _Connect()
        Case $idExit
            MsgBox(4096, $sTitle, "Exit was selected")
            _Quit()
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

Func _Quit()
    GUIDelete($hGUI)                                ; Delete the previous GUI and all controls.
    Exit
EndFunc

As always any help will be greatly appreciated.

Words of Wisdom and Favourite Quotes:

'Nothing is impossible, despite what other people say and think, prove them wrong and show them what you can do!'

'Understanding is a three edged sword, your side, their side and the truth!'

'The truth that humanity has never been able to grasp, is that death may be the only absolute freedom there is!'

'I've run out of places to put the brush... what do you suggest?'

  • Solution
Posted
Posted

@Danyfirex, thank you that worked!!!

Much appreciated.

Words of Wisdom and Favourite Quotes:

'Nothing is impossible, despite what other people say and think, prove them wrong and show them what you can do!'

'Understanding is a three edged sword, your side, their side and the truth!'

'The truth that humanity has never been able to grasp, is that death may be the only absolute freedom there is!'

'I've run out of places to put the brush... what do you suggest?'

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