How to include the same gui several times in a script? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

How to include the same gui several times in a script?

14 Jan 2019, 14:16

Hi,

The first script works perfectly:
Main script:

Code: Select all

#SingleInstance force
;TestScript for Guis

F1::
title       = Activated by F1
description = A MessageBox will apear and show the result
#include Guis\SimpleGui.ahk ; Include TestGui - Variable = input
MsgBox, %input%
return
Included script:

Code: Select all

Gui -Caption +LastFound +ToolWindow +AlwaysOnTop ; borderless, last found, no taskbar icon, always on top
Gui, Add, Picture, x0 y0 w600 h240, %A_ScriptDir%\Guis\Standard.jpg ; Background image (Image)
Gui, Add, Text, BackgroundTrans Center x0 y10 w600 h30 cWhite, %title% ; Title of the script (Text)
Gui, Add, Text, BackgroundTrans x10 y60 w500 h90 cWhite, %description% ; Descriptions (Text)
Gui, Add, Edit, vInput x10 y85 w580 ; Input field (Edit)

Gui, Add, Button, Default x195 y150 w80 gOK, OK ; Proceed with script (Button)
Gui, Add, Button, x295 y150 w80 gCancel, Cancel ; Cancel script (Button)

Gui, Show, w600 h240, Gui-Inputbox ; Show Gui
GuiControl, Focus, Input ; Focus input field automatically

Hotkey, Enter, OK ; Pressing Enter = OK
Hotkey, Escape, Cancel ; Pressing Esc = Cancel (DOES NOT WORK)
return

Cancel: ; When Cancel was chosen:
Gui, Destroy ; Destroy Gui
return

OK: ; When OK was chosen:
Gui, Submit ; Submit data
Gui, Destroy ; Destroy Gui
- I press F1
- type in a word inside of the edit field
- I press OK and get the word shown in a MessageBox - perfect.

When I want to add another sub-script in the main script, which should also access this gui, it doesn't work.

Code: Select all

F2::
title       = Activated by F2
description = A MessageBox will apear and show the result
#include Guis\SimpleGui.ahk ; Include TestGui - Variable = input
MsgBox, %input%
return
When pressing F2, the MessageBox appears immediatelly.
It seems I cannot include the same gui several times by using this method.

My next idea was to create a function inside of the SimpleGui.ahk

Code: Select all

Simple_Gui(title, description)
{
  Gui -Caption +LastFound +ToolWindow +AlwaysOnTop ; borderless, last found, no taskbar icon, always on top
  Gui, Add, Picture, x0 y0 w600 h240, %A_ScriptDir%\Guis\Standard.jpg ; Background image (Image)
  Gui, Add, Text, BackgroundTrans Center x0 y10 w600 h30 cWhite, %title% ; Title of the script (Text)
  Gui, Add, Text, BackgroundTrans x10 y60 w500 h90 cWhite, %description% ; Descriptions (Text)
  Gui, Add, Edit, vInput x10 y85 w580 ; Input field (Edit)

  Gui, Add, Button, Default x195 y150 w80 gOK, OK ; Proceed with script (Button)
  Gui, Add, Button, x295 y150 w80 gCancel, Cancel ; Cancel script (Button)

  Gui, Show, w600 h240, Gui-Inputbox ; Show Gui
  GuiControl, Focus, Input ; Focus input field automatically
}

;Hotkey, Enter, OK ; Pressing Enter = OK - doesn't matter if commented out or in
;Hotkey, Escape, Cancel ; Pressing Esc = Cancel (DOES NOT WORK) - doesn't matter if commented out or in

Cancel: ; When Cancel was chosen:
Gui, Destroy ; Destroy Gui
return

OK: ; When OK was chosen:
Gui, Submit ; Submit data
Gui, Destroy ; Destroy Gui
After updating my first script to:

Code: Select all

#SingleInstance force
;TestScript for Guis

F1::
#include Guis\SimpleGui.ahk ; Include TestGui - Variable = input
Simple_Gui("Activated by F1", "A MessageBox will apear and show the result") ; title, description | Variable = input
MsgBox, %input%
return

F2::
#include Guis\SimpleGui.ahk ; Include TestGui - Variable = input
Simple_Gui("Activated by F2", "A MessageBox will apear and show the result") ; title, description | Variable = input
MsgBox, %input%
return
it says: "parameters of hotkey functions must be optional" when trying to compile the script.

How to do it correctly?
Or better: How to include the same gui several times?
I prefer not to paste the whole gui code, because my guis usually take a whole site and it becomes confusing more quickly.
At the moment, I'm working on different types of guis which I would like to implement in my main script whenever I need them.

Perhaps using classes?
Thanks for any help!

Best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: How to include the same gui several times in a script?

14 Jan 2019, 14:34

#Include is almost like copying and pasting directly the code of your include file in the place you put the #include.
So in your last attempt you're trying to define a function inside a hotkey.
If you want to use a function inside your include you'd have to put the include in a place where you'd define a function in normal circumstances, and only once because you can only define a function name once.

If you wanted to do what you where trying at first, under #include documentation there is #IncludeAgain, it should work for your case.

EDIT: You could have problems with not being able to have those 2 guis open at the same time because they don't have a different name so probably overwrites the other.
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to include the same gui several times in a script?

14 Jan 2019, 16:07

Thanks for the answer.

Yes, I completely forgot that I'm basically putting a function inside of a hotkey, instead of calling it.
I tried your idea with #IncludeAgain, but I receive the error message: "Duplicate label".
Probably it does not work because AHK sees two times the same label (G-Label) inside of 1 script.

I tried it by adding the whole gui as function inside of the main script.

Code: Select all

#SingleInstance force
global abortScript

F1::
Simple_Gui("Activated by F1", "A MessageBox will apear and show the result")
if abortScript = 1 ; If Cancel was chosen:
{
  return ; Abort script
} ; Otherwise:
MsgBox, %input% ; Show value
return

F2::
Simple_Gui("Activated by F2", "A MessageBox will apear and show the result")
if abortScript = 1 ; If Cancel was chosen:
{
  return ; Abort script
} ; Otherwise:
MsgBox, %input% ; Show value
return

Simple_Gui(title, description)
{  
  Gui -Caption +LastFound +ToolWindow +AlwaysOnTop ; borderless, last found, no taskbar icon, always on top
  Gui, Add, Picture, x0 y0 w600 h240, %A_ScriptDir%\Guis\Standard.jpg ; Background image (Image)
  Gui, Add, Text, BackgroundTrans Center x0 y10 w600 h30 cWhite, %title% ; Title of the script (Text)
  Gui, Add, Text, BackgroundTrans x10 y60 w500 h90 cWhite, %description% ; Descriptions (Text)
  Gui, Add, Edit, vInput x10 y85 w580 ; Input field (Edit)

  Gui, Add, Button, Default x195 y150 w80 gOK, OK ; Proceed with script (Button)
  Gui, Add, Button, x295 y150 w80 gCancel, Cancel ; Cancel script (Button)

  Gui, Show, w600 h240, Gui-Inputbox ; Show Gui
  GuiControl, Focus, Input ; Focus input field automatically
  
  Hotkey, Enter, OK
  Hotkey, Escape, Cancel ; Pressing Esc = Cancel (DOES NOT WORK)
  return
  
  Cancel: ; When Cancel was chosen:
  Gui, Destroy ; Destroy Gui
  abortScript = 1
  return

  OK: ; When OK was chosen:
  Gui, Submit ; Submit data
  Gui, Destroy ; Destroy Gui
  return
}
However, there are 4 problems:

1)
When I run it the first time (e.g. pressing F1) and type in 1, the MessageBox is empty.
After running it again (doesn't matter if F1 or F2), the MessageBox shows 1 - no matter what I type in.
So it never shows the current value, only the past one.
On the 3rd round it would show me 2 if I typed in a 2 in the 2nd round.

2)
The focus doesn't work anymore.
After the gui gets loaded, there's a focus on the edit field for about 100 ms, but it disappears and I have to manually click inside of it.

3)
Enter key is blocked after closing the gui.
When I focus the edit field, type in a value and press Enter, the gui disappears (which is correct).
But then the Enter key is blocked until I terminate the whole script.

4)
There seems no way to cancel the script.
Doesn't matter if I press Esc or click Cancel, the MessageBox always appears.
That's why I added the abortScript variable, but it doesn't work...

Thanks for any help!

Regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: How to include the same gui several times in a script?  Topic is solved

14 Jan 2019, 17:31

When I run it the first time (e.g. pressing F1) and type in 1, the MessageBox is empty
After running it again (doesn't matter if F1 or F2), the MessageBox shows 1 - no matter what I type in.
So it never shows the current value, only the past one.
On the 3rd round it would show me 2 if I typed in a 2 in the 2nd round.
Your MsgBox was behind the Gui without you noticing it, this is because GUIs don't work the same as MsgBox.
A gui/window is created, and once it's created the program moves on leaving the window there.
If you want the script to wait you have to put a WinWaitClose at the end. More or less like this:

Code: Select all

WinWaitClose, % "ahk_id " WinExist() ; <--- Catches last found Hwnd
Or rework your script to show the message box where you handle the Submit.
The focus doesn't work anymore.
After the gui gets loaded, there's a focus on the edit field for about 100 ms, but it disappears and I have to manually click inside of it.
Could be the msgbox fault. I'd check if it's still a problem after fixing #1.
Enter key is blocked after closing the gui.
When I focus the edit field, type in a value and press Enter, the gui disappears (which is correct).
But then the Enter key is blocked until I terminate the whole script.
Hotkeys are applied globally, ahk doesn't know that you only want to apply them to one window until you tell it.
Check the hotkey commands to make the hotkeys only apply to your window.
There seems no way to cancel the script.
Doesn't matter if I press Esc or click Cancel, the MessageBox always appears.
That's why I added the abortScript variable, but it doesn't work...
Similar to #1.
User avatar
Scr1pter
Posts: 1275
Joined: 06 Aug 2017, 08:21
Location: Germany

Re: How to include the same gui several times in a script?

14 Jan 2019, 18:33

Hey, thanks!
It actually works fine now - no issues.
Great.

Code: Select all

#SingleInstance force
;TestScript for Guis

F1::
Simple_Gui("Activated by F1", "A MessageBox will apear and show the result") ; Run Gui-Infobox, title, description -> Variable = input
WinWaitClose, % "ahk_id " WinExist() ; Wait until the currently opened Gui was closed
if abortScript = 1 ; If cancel was pressed:
{
  return ; Abort script
}
MsgBox, %input% ; Show result in MessageBox
return

F2::
Simple_Gui("Activated by F2", "A MessageBox will apear and show the result") ; Run Gui-Infobox, title, description -> Variable = input
WinWaitClose, % "ahk_id " WinExist() ; Wait until the currently opened Gui was closed
if abortScript = 1 ; If cancel was pressed:
{
  return ; Abort script
}
MsgBox, %input% ; Show result in MessageBox
return

Simple_Gui(title, description) ; Gui-Infobox, title, description -> Variable = input
{  
  global abortScript ; 1 = Abort main routine, 0 = Continue main routine
  Gui -Caption +LastFound +ToolWindow +AlwaysOnTop ; borderless, last found, no taskbar icon, always on top
  Gui, Add, Picture, x0 y0 w600 h240, %A_ScriptDir%\Guis\Standard.jpg ; Background image (Image)
  Gui, Add, Text, BackgroundTrans Center x0 y10 w600 h30 cWhite, %title% ; Title of the script (Text)
  Gui, Add, Text, BackgroundTrans x10 y60 w500 h90 cWhite, %description% ; Descriptions (Text)
  Gui, Add, Edit, vInput x10 y85 w580 ; Input field (Edit)

  Gui, Add, Button, Default x195 y150 w80 gOK, OK ; Proceed with script (Button)
  Gui, Add, Button, x295 y150 w80 gCancel, Cancel ; Cancel script (Button)

  Gui, Show, w600 h240, Gui-Inputbox ; Show Gui
  GuiControl, Focus, Input ; Focus input field automatically
  
  Hotkey, Escape, Cancel ; Pressing Esc = Cancel
  return
  
  Cancel: ; When Cancel was chosen:
  Gui, Destroy ; Destroy Gui
  abortScript = 1 ; Abort main routine
  return

  OK: ; When OK was chosen:
  Gui, Submit ; Submit data
  Gui, Destroy ; Destroy Gui
  abortScript = 0 ; Continue main routine
  return
}
P.S. I don't need Hotkey, Enter inside of the gui, because the field is focused anyway.

Edit
It also works with #include Guis\SimpleGui.ahk ; Include TestGui - Variable = input instead of putting the whole function their,
but I was sure it would work. :)

Best regards
Please use [code][/code] when posting code!
Keyboard: Logitech G PRO - Mouse: Logitech G502 LS - OS: Windows 10 Pro 64 Bit - AHK version: 1.1.33.09

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Aqualest and 340 guests