Jump to content

Trying to understand GUICreate, GUIDelete, SW_HIDE and SW_SHOW


gminnick
 Share

Recommended Posts

I have been using AutoIT for awhile but have been wondering if I understand the Windowing concept.

Here is an example of what I am doing

I have a main Window (WindowA) with Button1 that when pressed will call a Function (Function1) that creates another Window (WindowB).

When I am done with WindowB I have a Close button that will hide the window.

I am now back in WindowA. If I click Button1 again, will it create another WindowB or does it know it is already created and just show it?

I am wondering if I have many hidden windows because of the fact that every time I call Function1 I am creating another window.

Should I create all my windows in the main part of the code and Show and Hide when needed?

I have some windows that have Lists that I want to refresh each time I call the window.

I tried to search the forums for this but this is a hard question to search for.

Thanks,

Link to comment
Share on other sites

I have been using AutoIT for awhile but have been wondering if I understand the Windowing concept.

Here is an example of what I am doing

I have a main Window (WindowA) with Button1 that when pressed will call a Function (Function1) that creates another Window (WindowB).

When I am done with WindowB I have a Close button that will hide the window.

I am now back in WindowA. If I click Button1 again, will it create another WindowB or does it know it is already created and just show it?

Upon calling GUICreate you're creating a new GUI from scratch, not an existing one of-course.

I am wondering if I have many hidden windows because of the fact that every time I call Function1 I am creating another window.

Should I create all my windows in the main part of the code and Show and Hide when needed?

I have some windows that have Lists that I want to refresh each time I call the window.

I tried to search the forums for this but this is a hard question to search for.

Thanks,

I'd say, if this window may be invoked more frequently then it may be clever to just show/hide the window instead of creating and destroying the window over and over. Otherwise, there is no reason to create another window which may not be invoked at all or just few times. You're deciding the correct path, if applicable ;]

Link to comment
Share on other sites

I have a main Window (WindowA) with Button1 that when pressed will call a Function (Function1) that creates another Window (WindowB).

When I am done with WindowB I have a Close button that will hide the window.

I am now back in WindowA. If I click Button1 again, will it create another WindowB or does it know it is already created and just show it?

Here, everything depends on you. You can can create a new window each time you press the button, and you can block it. If you do not control the existence of the window, then every time you click you will create a new window. It is the program does not understand.
Link to comment
Share on other sites

Thanks for the information. I am having trouble understanding the logic when I try to create the window and then show it when I need to. Here is a simplified version of what I am trying to do. It does not work correctly but at least it gives some example of where I need help.

When I run this and click Connect it will bring up the Authentication window. But if I click Close and then Connect again, it goes straight to the AuthenticationProcess function as though I clicked Connect on the main menu and Continue on the Authenticate Menu and it keeps calling the AuthenticationProcess over and over.

When I show an already created menu I am confused about where the While loop should be. I know I don't want to re-create the window and all the buttons again so I made an If Then statement to check for the existence of the window first.

Thanks for any help. Are there any tutorials that go into how this process works?

#include <GUIConstantsEX.au3>

#include <EditConstants.au3>

#include <StaticConstants.au3>

#include <ListboxConstants.au3>

#include <ListViewConstants.au3>

#include <ButtonConstants.au3>

#include <WindowsConstants.au3>

;Define Global Window variables

Dim $mainwindow, $welcomewindow, $authenticatewindow, $GetCompSchedWindow, $SchedJobsWindow

;Define Global variables

Dim $mwAddServerButton, $mwAuthenticate, $mwRefreshComputerbutton, $mwComputerSchedbutton, $mwSchedJobsbutton, $MWCancelbutton

;Create Main Menu

$mainwindow = GUICreate("Main Menu", 400, 500)

GUISetState(@SW_SHOW)

GUICtrlCreateLabel ("Main Menu ", 10, 20, 380, 20, $SS_Center)

GUICtrlSetFont (-1,12,600)

GUICtrlCreateLabel ("Server List", 15, 60, 100)

GUICtrlSetFont (-1,10,600)

$mwAuthenticate = GUICtrlCreateButton("Connect", 200, 60, 150, 30)

GUICtrlSetFont (-1,12,600)

$MWCancelbutton = GUICtrlCreateButton("Close", 200, 400, 150, 30)

GUICtrlSetFont (-1,12,600)

While 1

$msg = GUIGetMsg()

Select

Case $msg = $mwAuthenticate

AuthenticateMenu()

Case $msg = $MWcancelbutton

CLOSEClicked()

;ExitLoop

Case $msg = $GUI_EVENT_CLOSE

CLOSEClicked()

;ExitLoop

EndSelect

WEnd

Func ShowMainMenu2()

GUISetState(@SW_HIDE)

GUISwitch($mainwindow)

GUISetState(@SW_SHOW)

EndFunc

Func AuthenticateMenu()

Local $tbUserName, $tbPassword, $tbAltirisServer, $btnContinue, $btnCancel, $intAuthResult

If $authenticatewindow Then

GUISwitch($authenticatewindow)

GUISetState(@SW_SHOW)

Else

$authenticatewindow = GUICreate("Authentication Process", 400, 400)

MsgBox(0,"","Auth Window Created: " & $authenticatewindow)

GUISwitch($authenticatewindow)

GUISetState(@SW_SHOW)

GUICtrlCreateLabel ("Enter Credentials for connecting to Altiris Server", 10, 30, 380, 20, $SS_Center)

GUICtrlSetFont (-1,12,600)

GUICtrlCreateLabel ("User Name", 30, 80)

GUICtrlSetFont (-1,12,600)

$tbUserName = GUICtrlCreateInput ("", 150, 80, 100, 20)

GUICtrlSetState(-1,$GUI_FOCUS)

GUICtrlCreateLabel ("Password ", 30, 120, 100)

GUICtrlSetFont (-1,12,600)

$tbPassword = GUICtrlCreateInput ("", 150, 120, 100, 20, $ES_PASSWORD)

GUICtrlCreateLabel ("Altiris Server", 30, 160, 100)

GUICtrlSetFont (-1,12,600)

$tbAltirisServer = GUICtrlCreateCombo ( "Server1", 150, 160, 150, 20); create first item

GUICtrlSetData(-1,"Server2|Server3") ; add other item

GUICtrlCreateLabel ("Enter SA credentials for connecting to Altiris server.",30, 200)

GUICtrlCreateLabel ("You do not need to enter Domain as it will be based on Altiris server selected.",30, 220)

$btnContinue = GUICtrlCreateButton("Continue", 150, 270, 100, 30, $BS_DEFPUSHBUTTON)

GUICtrlSetFont (-1,12,600)

$btnCancel = GUICtrlCreateButton("Cancel", 150, 320, 100)

GUICtrlSetFont (-1,12,600)

EndIf

$msg = 0

While 1

$msg = GUIGetMsg()

Select

Case $msg = $btnContinue

$intAuthResult = AuthenticateProcess($tbUserName, $tbPassword, $tbAltirisServer)

If $intAuthResult Then ExitLoop

Case $msg = $btnCancel

ShowMainMenu2()

ExitLoop

Case $msg = $GUI_EVENT_CLOSE

ShowMainMenu2()

ExitLoop

EndSelect

WEnd

EndFunc

Func AuthenticateProcess($tbUserName, $tbPassword, $tbAltirisServer)

Local $strUserName, $strPassword, $strAltirisServer, $strDomain, $intResult

SplashTextOn("Connect to Server", "Connecting.....", 300, 50, -1, -1, 0, "", 14)

Sleep(5000)

SplashOff()

Return $intResult

EndFunc

Func CLOSEClicked()

;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE,

Exit

EndFunc

Link to comment
Share on other sites

  • Moderators

gminnick,

In this case, I would just create and then delete the authentication window. In my opinion you do not gain a great deal by keeping created windows hidden, unless you are going to use them very often or they are very complex and contain ListViews and the like.

Look at the following which I hope does what you want (I have stripped out a lot of the code to simplify):

#include <GUIConstantsEX.au3>
#include <StaticConstants.au3>

;Define Global Window variables
Dim $mainwindow, $welcomewindow, $authenticatewindow, $GetCompSchedWindow, $SchedJobsWindow

;Define Global variables
Global $mwAddServerButton, $mwAuthenticate, $mwRefreshComputerbutton, $mwComputerSchedbutton, $mwSchedJobsbutton, $MWCancelbutton

;Create Main Menu
$mainwindow = GUICreate("Main Menu", 400, 500)

GUICtrlCreateLabel("Main Menu ", 10, 20, 380, 20, $SS_Center)
GUICtrlSetFont(-1, 12, 600)

$mwAuthenticate = GUICtrlCreateButton("Connect", 200, 60, 150, 30)
GUICtrlSetFont(-1, 12, 600)

$MWCancelbutton = GUICtrlCreateButton("Close", 200, 400, 150, 30)
GUICtrlSetFont(-1, 12, 600)

GUISetState(@SW_SHOW)

While 1
    
    Switch GUIGetMsg()
        Case $mwAuthenticate
            AuthenticateMenu()
        Case $MWCancelbutton, $GUI_EVENT_CLOSE
            CLOSEClicked()
    EndSwitch
WEnd

Func AuthenticateMenu()

    Local $tbUserName, $tbPassword, $tbAltirisServer, $btnContinue, $btnCancel, $intAuthResult

    GUISetState(@SW_HIDE, $mainwindow)

    $authenticatewindow = GUICreate("Authentication Process", 400, 400)

    $btnContinue = GUICtrlCreateButton("Continue", 150, 270, 100, 30)
    GUICtrlSetFont(-1, 12, 600)
    $btnCancel = GUICtrlCreateButton("Cancel", 150, 320, 100)
    GUICtrlSetFont(-1, 12, 600)
    
    GUISetState(@SW_SHOW)
    
    While 1
        Switch GUIGetMsg()
            Case $btnContinue
                $intAuthResult = AuthenticateProcess($tbUserName, $tbPassword, $tbAltirisServer)
                If $intAuthResult Then ContinueCase ; Just carry on with the code for the next case
            Case $btnCancel, $GUI_EVENT_CLOSE
                GUIDelete($authenticatewindow)
                GUISetState(@SW_SHOW, $mainwindow)
                Return 
        EndSwitch
    WEnd


EndFunc  ;==>AuthenticateMenu

Func AuthenticateProcess($tbUserName, $tbPassword, $tbAltirisServer)

    SplashTextOn("Connect to Server", "Connecting.....", 300, 50, -1, -1, 0, "", 14)
    Sleep(1000)
    SplashOff()
    Return 1; To activate the ContinueCase - set to 0 if you want to simulate no connection

EndFunc  ;==>AuthenticateProcess

Func CLOSEClicked()
    Exit
EndFunc  ;==>CLOSEClicked

If you want tutorials, you will find good ones here and here.

M23

P.S. Please use Code tags. Put [code ] before and [/code ] after your posted code (but omit the trailing space - it is only there so the tags display here). It is much easier to read that way. :-)

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks for the answer. I actually do have some windows that have Listviews and that could be called over and over. I just picked one to use as an example. I have functions to add servers to a list, retrieve server information, etc... So I would like to learn how to show and hide windows that would be called several times. But overall this is a small program and I could live with creating and deleting. I will check out the tutorials.

The program is a front end to a product called Altiris that we use for automating our builds. So I have to connect to the Altiris server and provide routines to add a server to the Deployment Console, get a list of current jobs waiting to run on the servers, and add jobs to the server. Luckily I they have an SDK and when I learned AutoIt could use COM I started on this project, but have very limited understanding of how Windows really work.

Thanks again,

Link to comment
Share on other sites

  • Moderators

gminnick,

Here is a short example of using HIDE/SHOW I wrote for someone else on the forums. I hope you find it useful:

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

$Form1 = GUICreate("Window 1", 420, 160)
$Button11 = GUICtrlCreateButton("Next", 355, 125, 60, 30)

GUISetState(@SW_SHOW)

$Form2 = GUICreate("Window 2", 420, 160)
$Button21 = GUICtrlCreateButton("Back", 290, 125, 60, 30)
$Button22 = GUICtrlCreateButton("Next", 355, 125, 60, 30)
GUISetState(@SW_HIDE)

$Form3 = GUICreate("Window 3", 420, 160)
$Button31 = GUICtrlCreateButton("Back", 290, 125, 60, 30)
$Button32 = GUICtrlCreateButton("Exit", 355, 125, 60, 30)


GUISetState(@SW_HIDE)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button11
            _Form2()
    EndSwitch
WEnd


Func _Form2()
    GUISetState(@SW_HIDE, $Form1)
    GUISetState(@SW_SHOW, $Form2)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button21
                GUISetState(@SW_HIDE, $Form2)
                GUISetState(@SW_SHOW, $Form1)
                Return
            Case $Button22
                _Form3()
        EndSwitch
    WEnd

EndFunc  ;==>_Form2
;new
Func _Form3()

    GUISetState(@SW_HIDE, $Form2)
    GUISetState(@SW_SHOW, $Form3)

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button31
                GUISetState(@SW_HIDE, $Form3)
                GUISetState(@SW_SHOW, $Form2)
                Return
            Case $Button32
                Exit
                Return
        EndSwitch
    WEnd

EndFunc  ;==>_Form3

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks Melba23. I think I am understanding this better with the 2 examples I have been given. It seems to make this work, you need to create the Windows outside of the functions along with their controls. And any variables assigned to those controls must be global. And the functions just handle Hiding/Showing along with the while loop to capture input for the Window you SHOW.

I have been trying to logically think how the process would work, but sometimes you don't really know what is going on under the covers, but it seems pretty straightforward once I see some examples.

Thanks to everyone that replied. Each reply helped answer my questions. And thanks for taking the time to reply so quickly. Very much appreciated.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...