Gui Only When Needed

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Builder01
Posts: 1
Joined: 29 Aug 2016, 14:41

Gui Only When Needed

29 Aug 2016, 15:44

Please Understand... I am an "almost" Newbie...

For those of you who do not want to make a Gui when your script start, because you may not need it...

I may be going against the grain here...

But I have a script that may not ever use a Gui... and when it does, it is a one time use, and discarded.

All the examples for Gui's I have seen create the Gui at the start of the script, and then the script is Gui driven. There are occasions when one does not want to do that.

So how then, to call the Gui later... As soon as you show the Gui, it "returns" to the point from which it is called, before the user has been able to use it.

I believe what it does then is fall back into itself if there is no code below the calling point, or executes the next line of code after the point of the Gui being called.

More or less, standard Gosub operation... except for the problem of not being able to use the Gui where you want it.

I looked all over for a remedy to this, but found nothing. Then when I realized how it was behaving, and thought of the answer... Control when it falls.

It works like this.. when you want the Gui, you Gosub to a Gui Maker routine, which resides immediately above the actual Gui.

It calls the Gui... and if the user is not finished with the Gui, it continues to "fall" back into the Gui. When the user is finished (determined by a variable set by operating the Gui), the GuiMaker subroutine returns control back to line in the script immediately after it's call.

The Guru's almost certainly have a better way to do it, but this works for me.

Code: Select all

;line of script 
;line of script
Gosub Gui_Maker_Subroutine

Gui_Maker_Subroutine:
Gui_Used=0                             ;Set the Flag used to Determine if the User is Finished with the Gui
Gosub The_Gui
Loop                                       ;The Check must be in a loop or else it will fail
    {
    If (Gui_Used)
         return
     }

The_Gui:
    Gui, Add, Button, gButton_Action
    Gui, Show
    return

Button_Action:
   Gui_Used=1                             ;If the action of the Button should close the Gui, Set the Flag
   ; Your Gui Action Here
   Gui, Destroy                             ; Since the whole idea is that you will not need this Gui again, destroy it, and free up resources
   return
I hope this helps all those folks looking for this.

Guru's, please let me know how you do it. Thanks
User avatar
ahkDustVorteX
Posts: 47
Joined: 14 May 2014, 12:08

Re: Gui Only When Needed

19 Sep 2016, 13:55

Hey Builder01,

This is kinda of help request, isn't it? You should remake your post in "ask for help" section.

Anyway, building GUI and rebuilding is messy and not effective. You should try making a full GUI a make it with visible property off. When you want to use it, you just enable it.
Editor: Notepad++
"Those who wait and persist, always achieve."
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Gui Only When Needed

19 Sep 2016, 18:48

That's quite a mess, in fact. I'm not understanding what you're trying to do exactly.

Code: Select all

gui,add,text,,This is a default GUI.
gui,add,button,gguiClose,Close GUI
return

guiClose:
gui,cancel
return

F1::gui,show,,test GUI
Gosub runs a subroutine, then when a return is encountered, it jumps back to where gosub was called. Meaning, it would run Gui_Maker_Subroutine: immediately after it returns, because it would fall through into the label, since there is no return after the gosub.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Gui Only When Needed

19 Sep 2016, 21:19

I usually use WinWaitClose.

Code: Select all

Gosub, Gui_Maker_Subroutine
MsgBox, Hi
return

Gui_Maker_Subroutine:
	Gui, +HWNDhGUI
	Gui, Add, Button, , 
	Gui, Show, w400 h100

	WinWaitClose, ahk_id %hGUI%
	Gui, Destroy
Return
If you have to wait for a variable to change, then it is better to add some Sleep to the Loop.

Code: Select all

while (Gui_Used) {
	Sleep, 10
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Panaku, Rohwedder, roysubs and 329 guests