How to check if Gui is empty before showing it?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
think
Posts: 137
Joined: 09 Feb 2014, 05:20

How to check if Gui is empty before showing it?

09 May 2024, 15:10

I'm creating a dynamic gui and sometimes it does not contain any control. How can check if gui is empty before it is shown? Checking it's size? I would like to avoid a taskbar showing an empty gui.

Code: Select all

gui, 2:destroy
gui, 2:Show,, MyGui 
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: How to check if Gui is empty before showing it?

09 May 2024, 15:51

Hello,

This is simple because the script controls the GUI. Therefore, your script always knows exactly what controls have been added. You can track that with a variable, use GuiControlGet, etc.
User avatar
boiler
Posts: 17189
Joined: 21 Dec 2014, 02:44

Re: How to check if Gui is empty before showing it?

09 May 2024, 18:15

An alternate approach is to use this function to which you pass the GUI's hwnd. It will work without showing the GUI if you have DetectHiddenWindows on.

Code: Select all

CtrlCount(id) {
	WinGet, list, ControlList, ahk_id %id%
	RegExReplace(list, "\w+",, count)
	return count
}

Demonstration:

Code: Select all

DetectHiddenWindows, On
Gui, +HwndGuiID
;Gui, Show
MsgBox, % "No. of controls: " CtrlCount(GuiID)

Gui, Add, Text,, Hello
MsgBox, % "No. of controls: " CtrlCount(GuiID)

Gui, Add, Button, xm, Button
MsgBox, % "No. of controls: " CtrlCount(GuiID)
ExitApp

CtrlCount(id) {
	WinGet, list, ControlList, ahk_id %id%
	RegExReplace(list, "\w+",, count)
	return count
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: LazyVovchik, peter_ahk, Rohwedder, rubeusmalfoy and 90 guests