Gui, %A_Gui%:Destroy vs. Gui, Destroy Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
freespacing
Posts: 131
Joined: 28 Sep 2016, 11:14
Contact:

Gui, %A_Gui%:Destroy vs. Gui, Destroy

13 Jan 2020, 12:51

I'm only now starting to dive into the marvelous world of GUIs.

Regarding event handlers such as GUIClose, I have a question: within GUIClose, if several Guis are running, is Gui, unambiguous?
(I.e. is there ever a need to specify the GUI name or number?) I'm assuming that since it's a handler, Gui, only applies to the relevant Gui and that A_Gui is unnecessary (except if conditional branching is desired, of course.)
Let me illustrate.

Let's assume a named GUI called GuiBlack. We assign a prefix to the handlers via:

Code: Select all

Gui, GuiBlack:+LabelDestroyOn
Now we write the handler as a function:

Code: Select all

DestroyOnClose() {
   ; [X] icon was clicked
   Gui, Destroy
   ; THIS ALSO WORKS:
   ; Gui, %A_Gui%:Destroy
}
Both options work: Gui, Destroy or Gui, %A_Gui%:Destroy
I'm assuming that Gui, %A_Gui%:Destroy is overkill and never necessary because within the event handler, Gui, is unambiguous even if several Guis are running. Is that true?

Thanks again for your expert insight, I want to make sure to adopt best practices.
Last edited by freespacing on 13 Jan 2020, 15:13, edited 1 time in total.
User avatar
flyingDman
Posts: 456
Joined: 29 Sep 2013, 19:01
Location: Burbank, California

Re: Gui, %A_Gui%:Destroy vs. Gui, Destroy

13 Jan 2020, 14:38

Should it not be DestroyOnClose() instead of DestroyOnGUIClose()?
The help file is a bit confusing on this. In addition the help file does not specifically mention the use of functions, only labels.
The use of functions for Gui Events is mentioned here: https://www.autohotkey.com/docs/misc/Labels.htm
Last edited by flyingDman on 13 Jan 2020, 22:27, edited 1 time in total.
freespacing
Posts: 131
Joined: 28 Sep 2016, 11:14
Contact:

Re: Gui, %A_Gui%:Destroy vs. Gui, Destroy

13 Jan 2020, 15:12

flyingDman wrote:
13 Jan 2020, 14:38
Should it not be DestroyOnClose() instead of DestroyOnGUIClose()?
The help file is a bit confusing on this. In addition the help file does not specifically mention the use of functions, only labels.
Yes, you're absolutely right. Just discovered that the hard way.
Will edit the original question. Thank you for the correction.
User avatar
flyingDman
Posts: 456
Joined: 29 Sep 2013, 19:01
Location: Burbank, California

Re: Gui, %A_Gui%:Destroy vs. Gui, Destroy

13 Jan 2020, 17:09

There is still an issue though. Say this is what defines the gui:

Code: Select all

Gui test: +LabelMain
Gui test: Add, edit, w400 h200
Gui test: Show, ,Main
return
These 2 options should be identical:

Code: Select all

MainClose:
msgbox,,,closing...,1
return
and

Code: Select all

MainClose(){
msgbox,,,closing...,1
}
They are not. The first one does not hide the window, while the second does. Am I missing something?
gregster
Posts: 3623
Joined: 30 Sep 2013, 06:48

Re: Gui, %A_Gui%:Destroy vs. Gui, Destroy

15 Jan 2020, 00:14

flyingDman wrote:
13 Jan 2020, 17:09
The first one does not hide the window, while the second does. Am I missing something?
It's not described explicitly in the docs, but afaik it's similar to the the old OnExit command (with a label; not the function version) which needs an explicit ExitApp to actually exit the app: "Since the specified subroutine is called instead of terminating the script, that subroutine must use the ExitApp command if termination is desired."

This GuiClose label also gets "called instead" of closing the GUI and you need to decide what actually should be done (just hide the GUI, ExitApp or whatever), while the newer function version automatically hides the GUI by default:
https://www.autohotkey.com/docs/commands/Gui.htm#GuiClose wrote:[v1.1.20+]: If GuiClose is a function, the GUI is hidden by default. The function can prevent this by returning a non-zero integer [...]
The function version simply allows to prevent the closing of the GUI with a non-zero integer return value, but how would you prevent the closing of the GUI with the traditional GuiClose label event, if it would always close the GUI automatically ? Like this, it leaves you options.
just me
Posts: 6652
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Gui, %A_Gui%:Destroy vs. Gui, Destroy  Topic is solved

15 Jan 2020, 03:57

freespacing wrote:I'm assuming that Gui, %A_Gui%:Destroy is overkill and never necessary because within the event handler, Gui, is unambiguous even if several Guis are running. Is that true?
For GUI threads, yes:
GUI Events, Threads, and Subroutines wrote:
A GUI thread is defined as any thread launched as a result of a GUI action. GUI actions include selecting an item from a GUI window's menu bar, or triggering one of its g-labels (such as by pressing a button).
The default window name for a GUI thread is that of the window that launched the thread. Non-GUI threads use 1 as their default.
...
The built-in variables A_Gui and A_GuiControl contain the window name and Control ID that launched the current thread.

https://www.autohotkey.com/docs/commands/Gui.htm#DefaultWin
freespacing
Posts: 131
Joined: 28 Sep 2016, 11:14
Contact:

Re: Gui, %A_Gui%:Destroy vs. Gui, Destroy

15 Jan 2020, 07:32

Thank you very much, @just me !
Exactly the information I was looking for.

Wishing you all a great day.

Return to “Ask For Help”

Who is online

Users browsing this forum: adrian_9832, anadir, Bing [Bot], daywalker, Google [Bot], Odlanir, Xeo786 and 197 guests