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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
william_ahk
Posts: 486
Joined: 03 Dec 2018, 20:02

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

Post by william_ahk » 06 Dec 2021, 22:25

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?

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

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

Post by boiler » 06 Dec 2021, 23:14

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

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

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

Post by teadrinker » 07 Dec 2021, 03:21

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

Post Reply

Return to “Ask for Help (v1)”