Jump to content
Sign in to follow this  
IvanCodin

Three GUI Menus Scripts exits

Recommended Posts

IvanCodin

I am trying to write a script that has multiple GUI windows. I created an example of the script I am trying to run. I want the Main GUI to start when the script is ran. If you press the button to select the Second GUI it sends you to another form, and if you press a button in the second form it sends you to another gui form, in this case alt. If I comment out the $AltForm portion of the code its runs. Is what I am trying to do proper scripting technique?

CODE
#include <GUIConstantsEX.au3>

#include <Misc.au3>

#include <Process.au3>

;AutoItSetOption("MustDeclareVars", 1)

Opt('GUIOnEventMode',True)

; vars

HotKeySet("^x", "_Bye")

$my_version = "0.0.1"

Global $Width = ('499')

Global $Height = ('447')

$MainForm = GUICreate("Main", $Width, $Height, 270, 98)

GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit', $MainForm)

; =>Context Menu MainForm

$Context_Menu = GUICtrlCreateContextMenu()

$Item_1 = GUICtrlCreateMenuItem("About", $Context_Menu)

GUICtrlSetOnEvent($Item_1, "_About")

$Item_2 = GUICtrlCreateMenuItem("Exit", $Context_Menu)

GUICtrlSetOnEvent($Item_2, "_Exit")

GUICtrlCreateLabel("<====== My Notes 1 =====>", 205, 10)

; =>MainForm buttons

$cCommand1 = GUICtrlCreateEdit("", 120, 40, 365, 397)

$Button0 = GUICtrlCreateButton("Copy My Notes 1", 20, 46, 89, 23, 0)

GUICtrlSetOnEvent($Button0,"MyClip1")

$Button1 = GUICtrlCreateButton("Spare", 20, 77, 89, 23, 0)

GUICtrlSetOnEvent($Button1, "Spare")

$Button12 = GUICtrlCreateButton("Second GUI", 20, 418, 89, 23, 0)

GUICtrlSetOnEvent($Button12, "SecondGUI")

$SecondForm = GUICreate("Main", $Width, $Height, 270, 98)

GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit', $SecondForm)

; =>Context Menu SecondForm

$Context_Menu = GUICtrlCreateContextMenu()

$Item_1 = GUICtrlCreateMenuItem("About", $Context_Menu)

GUICtrlSetOnEvent($Item_1, "_About")

$Item_2 = GUICtrlCreateMenuItem("Exit", $Context_Menu)

GUICtrlSetOnEvent($Item_2, "_Exit")

GUICtrlCreateLabel("<====== My Notes 2 =====>", 205, 10)

; =>SecondForm buttons

$cCommand2 = GUICtrlCreateEdit("", 120, 40, 365, 397)

$Button0 = GUICtrlCreateButton("Copy My Notes 2", 20, 46, 89, 23, 0)

GUICtrlSetOnEvent($Button0,"MyClip1")

$Button21 = GUICtrlCreateButton("Spare Second-21", 20, 77, 89, 23, 0)

GUICtrlSetOnEvent($Button21, "Spare")

$Button30 = GUICtrlCreateButton("AltGUI", 20, 387, 89, 23, 0)

GUICtrlSetOnEvent($Button30, "AltGUI")

$Button31 = GUICtrlCreateButton("Return to Main", 20, 418, 89, 23, 0)

GUICtrlSetOnEvent($Button31, "ReturnMainGUI")

$AltForm = GUICreate("Main", $Width, $Height, 270, 98)

GUISetOnEvent($GUI_EVENT_CLOSE, '_Exit', $AltForm)

; =>Context Menu AltForm

$Context_Menu = GUICtrlCreateContextMenu()

$Item_1 = GUICtrlCreateMenuItem("About", $Context_Menu)

GUICtrlSetOnEvent($Item_1, "_About")

$Item_2 = GUICtrlCreateMenuItem("Exit", $Context_Menu)

GUICtrlSetOnEvent($Item_2, "_Exit")

GUICtrlCreateLabel("<====== My Notes =====>", 205, 10)

; =>AltForm buttons

;$cCommand1 = GUICtrlCreateEdit("", 120, 40, 365, 397)

;$Button0 = GUICtrlCreateButton("Copy My Notes 1", 20, 46, 89, 23, 0)

;GUICtrlSetOnEvent($Button0,"MyClip1")

$Button41 = GUICtrlCreateButton("Return 2nd Menu Alt-41", 20, 418, 89, 23, 0)

GUICtrlSetOnEvent($Button41, "SecondGUI")

GUISetState(@SW_SHOW, $MainForm)

While 1

$msg = GUIGetMsg()

If $msg = $Item_2 Or $msg = -3 Or $msg = -1 Then

ExitLoop

EndIf

Sleep(10) ; Idle around

WEnd

Func MainGUI()

;$Pos = WinGetPos($MainForm)

;WinMove($Mainform, "", $Pos[0], $Pos[1])

GUISetState(@SW_SHOW, $MainForm)

EndFunc ;==> MainGUI

Func SecondGUI()

$Pos = WinGetPos($MainForm)

WinMove($SecondForm, "", $Pos[0], $Pos[1])

GUISetState(@SW_Hide, $MainForm)

GUISetState(@SW_SHOW, $SecondForm)

; $msg2 = GUIGetMsg()

EndFunc ;==> SecondGUI

Func AltGUI()

$Pos = WinGetPos($SecondForm)

WinMove($AltForm, "", $Pos[0], $Pos[1])

GUISetState(@SW_Hide, $SecondForm)

GUISetState(@SW_SHOW, $AltForm)

; $msg2 = GUIGetMsg()

EndFunc ;==> AltGUI

Func ReturnMainGUI()

GUISetState(@SW_Hide, $SecondForm)

GUISetState(@SW_SHOW, $MainForm)

EndFunc ;==> ReturnMainGUI

Func Spare()

MsgBox(4096, "Info", "Not yet implemented", 3)

EndFunc ;==> Spare

Func MyClip1()

ClipPut("") ; clear to prevent duplicate entries

ClipPut(GUICtrlRead($cCommand1) & @CRLF)

If (@error == 1) Then

MsgBox(0, "", "Unable to copy to the ClipBoard.")

EndIf

;EndSelect

Sleep(2000)

EndFunc ; => Myclip1

Func MyClip2()

ClipPut("") ; clear to prevent duplicate entries

ClipPut(GUICtrlRead($cCommand2) & @CRLF)

If (@error == 1) Then

MsgBox(0, "", "Unable to copy to the ClipBoard.")

EndIf

;EndSelect

Sleep(2000)

EndFunc ; => Myclip2

Func _About()

MsgBox(0, "About", "Tool Box " & $my_version, 4)

EndFunc ;==>_About

Func _Bye()

SoundPlay(@WindowsDir & "\media\tada.wav",1)

MsgBox(0, "Bye", "Exiting program", 4)

Exit

EndFunc ;==>_Bye

Func _Exit()

Exit

EndFunc ;==>_Exit

Thanks

CC

Share this post


Link to post
Share on other sites
Authenticity

There is nothing wrong with this approach but keep in mind that if you don't want two GUI windows at the same time you can just use 3 cases the delete the necessary controls and make 3 functions that build the GUI differently. If this approach is applicable then it'd be preferable, though both approaches work great IMO.

Edited by Authenticity

Share this post


Link to post
Share on other sites
IvanCodin

So to fix my script I need to add a case statement for the GUI to be displayed correctly? The script will not run as I have written it. If I comment out the ALTGUI section in the script the Main and Second forms work correctly. Remove the comments and the scripts doesn't work correctly.

I had already written the script in message mode and converted to event mode when it appeared to be the best method for the script to run in. I am not sure but believe the event mode is the best method this script should use.

CC

Edited by IvanCodin

Share this post


Link to post
Share on other sites
Authenticity

OnEventMode can sometimes be the right choice and there is no best as far as I can say. Don't forget the a GUI window has only one close button which is handled by the event $GUI_EVENT_CLOSE so you need not concern about the general things and only about the client area like controls and the event they produce. As I see it, this case (your case) can be programmed easily by 3 aesthetical functions which get called upon event from different control they create so once $Run might refer to event that call case 1 while in different case it'll reference to an event that call case 3...

Share this post


Link to post
Share on other sites
IvanCodin

/edited for spelling and clarity

I guess I don't understand some of the GUI concepts fully. When I read the Help file it discussed the GUI design and how the GUI was created and deleted.

This is how I understand the GUI creation process works based on my understanding of the Help File.

The original script had two GUI's windows. It works without any problem:

$MainForm = GUICreate("Main", $Width, $Height, 270, 98)

main form stuff here

$SecondForm = GUICreate("Second", $Width, $Height, 270, 98)

second form stuff here

GUISetState(@SW_SHOW,$MainForm)

To switch between the GUI forms I would use:

GUISetState(@SW_HIDE,$MainForm)

GUISetState(@SW_SHOW,$SecondForm)

And to switch back I would use:

GUISetState(@SW_HIDE,$SecondForm)

GUISetState(@SW_SHOW,$MainForm)

T scripts starts and the desired GUI is shown using the command GUISetState (@SW_SHOW, $MainForm). The MainForm is correctly displayed.

I logically extended it to include and ALT form and assumed it would work like this:

$MainForm = GUICreate("Main", $Width, $Height, 270, 98)

main form stuff here

$SecondForm = GUICreate("Second", $Width, $Height, 270, 98)

second form stuff here

$AltForm = = GUICreate("Alt", $Width, $Height, 270, 98)

Alt form stuff here

GUISetState(@SW_SHOW,$MainForm)

While in the MainForm to switch between the Second form I would use:

GUISetState(@SW_HIDE,$MainForm)

GUISetState(@SW_SHOW,$SecondForm)

This works in my script if the alt form is commented out. If I was viewing the SecondForm and wanted to see the AltForm form I would use:

GUISetState(@SW_HIDE,$SecondForm)

GUISetState(@SW_SHOW,$AltForm)

And to switch back I would use:

GUISetState(@SW_HIDE,$AltForm)

GUISetState(@SW_SHOW,$SecondForm)

Problem is when I added the AltForm the script runs and dies immediately. I don't see the logic error in the script, it's on the first post, unless the GUI process doesn't work as I described it here. So I am missing something or the way I expect the GUI to behave is incorrect.

CC

Edited by IvanCodin

Share this post


Link to post
Share on other sites
Authenticity

Opt('MustDeclareVars', 1)
Opt('GUIOnEventMode', 1)

HotKeySet('{ESC}', '_Exit')

Dim $hGUI1, $hGUI2, $hGUI3, $btGoTo1, $btGoTo2, $btGoTo3

$hGUI1 = GUICreate('Graphical User Interface Main', 280, 255)
$btGoTo1 = GUICtrlCreateButton('GUI 2', 115, 115, 50, 24)
GUICtrlSetOnEvent($btGoTo1, 'SwitchGui')

$hGUI2 = GUICreate('Graphical User Interface 2', 280, 255)
$btGoTo2 = GUICtrlCreateButton('GUI 3', 115, 115, 50, 24)
GUICtrlSetOnEvent($btGoTo2, 'SwitchGui')

$hGUI3 = GUICreate('Graphical User Interface 3', 280, 255)
$btGoTo3 = GUICtrlCreateButton('Main', 115, 115, 50, 24)
GUICtrlSetOnEvent($btGoTo3, 'SwitchGui')

GUISetState(@SW_SHOW, $hGUI1)

While 1
    Sleep(20)
WEnd


Func _Exit()
    GUIDelete($hGUI3)
    GUIDelete($hGUI2)
    GUIDelete($hGUI1)
    Exit
EndFunc

Func SwitchGui()
    
    Switch @GUI_CtrlId
        
        Case $btGoTo1
            GUISetState(@SW_HIDE, $hGUI1)
            GUISetState(@SW_SHOW, $hGUI2)
    
        Case $btGoTo2
            GUISetState(@SW_HIDE, $hGUI2)
            GUISetState(@SW_SHOW, $hGUI3)
    
        Case Else
            GUISetState(@SW_HIDE, $hGUI3)
            GUISetState(@SW_SHOW, $hGUI1)
    EndSwitch
EndFunc

This a sample of 3 GUI windows, it might be something in the msg loop or the AltForm that crashes your program.

Share this post


Link to post
Share on other sites
IvanCodin

Thanks for the EXCELLENT example. :) I sure wish I know why the script I had initially written didn't work but will use the example you provided.

CC

Share this post


Link to post
Share on other sites
Authenticity

After looking again about the problem it might not be the ideal solution if you want to handle message loop because of the 'GuiOnEventMode'.

Maybe this one can be of help:

Opt('MustDeclareVars', 1)

HotKeySet('{ESC}', '_Exit')

Dim $hGUI1, $btGoTo2

$hGUI1 = GUICreate('Graphical User Interface Main', 280, 255)
$btGoTo2 = GUICtrlCreateButton('GUI 2', 115, 115, 50, 24)

GUISetState(@SW_SHOW, $hGUI1)

While 1
    Local $msg = GUIGetMsg()
    
    Switch $msg
        Case $btGoTo2
        GUISetState(@SW_HIDE, $hGUI1)
        CreateGui2()
        GUISetState(@SW_SHOW, $hGUI1)
        
        Case -3
            GUIDelete($hGUI1)
            Exit
        
    EndSwitch
    Sleep(20)
WEnd

Func CreateGui2()
    
    Local $hGUI2, $btGoTo3
    
    $hGUI2 = GUICreate('Graphical User Interface 2', 280, 255)
    $btGoTo3 = GUICtrlCreateButton('GUI 3', 115, 115, 50, 24)
    
    GUISetState(@SW_SHOW, $hGUI2)
    
    While 1
        Local $msg = GUIGetMsg()
        
        Switch $msg
            Case $btGoTo3
                GUISetState(@SW_HIDE, $hGUI2)
                CreateGui3()
                GUISetState(@SW_SHOW, $hGUI2)
                
            Case -3
                ExitLoop
            
        EndSwitch
    WEnd
    
    GUIDelete($hGUI2)
EndFunc

Func CreateGui3()
    
    Local $hGUI3
    
    $hGUI3 = GUICreate('Graphical User Interface 3', 280, 255)
    
    GUISetState(@SW_SHOW, $hGUI3)
    
    While 1
        Local $msg = GUIGetMsg()
        
        if $msg = -3 Then ExitLoop
    WEnd
    
    GUIDelete($hGUI3)
EndFunc

Func _Exit()
    Exit
EndFunc

It's totally useless for the purpose but the concept is valid.

Edited by Authenticity

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  

×