Page 1 of 1

Create a new GUI every time or show the previously created GUI?

Posted: 06 Dec 2021, 22:25
by william_ahk
For example:
Create and show every time:

Code: Select all

F1::
if (WinExist("ahk_id " hGUI)) {
    Gui, %hGUI%:Show
    return
}
Gui, New, +HwndhGUI
Gui, %hGUI%:Add, Text, , test
Gui, %hGUI%:Show
return
Create once and show every time:

Code: Select all

Gui, New, +HwndhGUI
Gui, %hGUI%:Add, Text, , test
return

F1::
Gui, %hGUI%:Show
return
Which design is generally better for extensibility and future maintenance? What are the things to look out for each design? Or are there better designs?

Re: Create a new GUI every time or show the previously created GUI?

Posted: 06 Dec 2021, 23:14
by boiler
The first one is not a great design because the old GUI is never destroyed, so you’re not cleaning up your resources. You end up with a bunch of hidden GUIs that exist and could be shown if you had a record of their HWNDs. It wouldn’t be that bad if you destroyed the old one before creating another one, but I don’t see the advantage over just showing the same one.

Sometimes I destroy a GUI and recreate in the case where I want it to start fresh with the controls as they were when first created (empty edit boxes, etc.).

Re: Create a new GUI every time or show the previously created GUI?

Posted: 07 Dec 2021, 03:21
by teadrinker
The first code could be improved like this:

Code: Select all

F1::
DetectHiddenWindows, On
if !WinExist("ahk_id" . hGUI) {
   Gui, New, +HwndhGUI
   Gui, Add, Text,, test
}
Gui, %hGUI%:Show, w200 h100
return