GUI duplicate script optimisation Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
KRG-23
Posts: 19
Joined: 07 Jun 2016, 05:45
Location: France

GUI duplicate script optimisation

02 Aug 2018, 07:04

Hello,

As a new poster in this board, I want to firstly say a huge "thank you" to its community that helped me thousands of times in coping with my poor scripting skills.
Secondly, I beseech you to help me as I have a problem that I can't seem to solve : I have a Gui1 that contains a button that duplicates Gui1 to Gui++. Gui1 and Gui++ are almost the same, Gui1 has a button that Gui++ hasn't. I can therefor have dozens of Guis that will serve me as stickynotes for a bigger project.

The GOSUB generating Gui++ is the same as Gui1 creation code and I find it difficult to have to update both codes everytime I change for say a button width or a font color.

What I would like you to help me with is by optimizing the Guis creation code that follows:

Code: Select all


#SingleInstance,Force

; ===============================================
; Variables
; ===============================================


; Main UI settings
; ===============================================
Gui_Name := 1 ; to number Gui ids
GuiMain_Xpos := 1000 ; used by duplicate window and forthcoming location function
GuiMain_Ypos := 100	; used by duplicate window and forthcoming location function



; ===============================================
; Main Gui layout
; ===============================================
Gui, %Gui_Name%: +Caption +Border
Gui, %Gui_Name%: Color, cFFFFFF
Gui, %Gui_Name%: Add, Text, x10 y10, [Instance%Gui_Name%]
Gui, %Gui_Name%: Add, Button, Section xp y+10 w100 h25 gButton_NewWindow, New Window
Gui, %Gui_Name%: Add, Button, -Tabstop ys wp hp gButton_Settings, Settings
Gui, %Gui_Name%: Add, Button, ys wp hp gGuiClose, &Close
Gui, %Gui_Name%: Show, x%GuiMain_Xpos% y%GuiMain_Ypos% w340 h70, Test
Return


; ===============================================
;Labels
; ===============================================

;New popup
; ===============================================
Button_NewWindow:
		Gui_Name++
		WinGetPos, Xpos, Ypos,, %Gui_Name%
		Xpos := Xpos + 50
		Ypos := Ypos + 25
		#SingleInstance off
		Gui, %Gui_Name%: +AlwaysOnTop +LastFound +Caption +Border	
		Gui, %Gui_Name%: New
		Gui, %Gui_Name%: Color, cFFFFFF
		Gui, %Gui_Name%: Add, Text, x10 y10, [Instance%Gui_Name%]
		Gui, %Gui_Name%: Add, Button, Section xp y+10 w100 h25 gButton_NewWindow, New Window
		Gui, %Gui_Name%: Add, Button, -Tabstop ys wp hp hidden gButton_Settings, Settings
		Gui, %Gui_Name%: Add, Button, ys wp hp gGuiClose_New, &Close
		Gui, %Gui_Name%: Show, x%Xpos% y%Ypos% w340 h70, Test
Return
	
GuiClose:
	ExitApp
	Return
	
GuiClose_New:
	WinGetTitle, Title, A
	PostMessage, 0x112, 0xF060,,, %Title%
	Return

Button_Settings:
	Return
	
; ===============================================	
;Functions
; ===============================================

I thank you for your help :+1:
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: GUI duplicate script optimisation

02 Aug 2018, 07:18

I don't know too much about the optimization, but you don't need the #SingleInstance Off as with Gui, New you are creating a new GUI window with each button press, so you aren't running multiple instances of the script, you are only running it once. One options I can think of that you could do is only have the 1 instance and turn SingleInstance off, and the new window button could just run the script again (creating the same instance again). You can set the Run command to run the script again and again, and if you need parameters send to it you can do something like this

Code: Select all

Run %A_AHKPath% "Test.ahk" Par1 Par2 .....
But it also depends on what you plan on doing with your button settings. Other than that I don't have anything else in mind to improve it.
User avatar
KRG-23
Posts: 19
Joined: 07 Jun 2016, 05:45
Location: France

Re: GUI duplicate script optimisation

02 Aug 2018, 08:09

MannyKSoSo wrote:I don't know too much about the optimization, but you don't need the #SingleInstance Off as with Gui, New you are creating a new GUI window with each button press, so you aren't running multiple instances of the script, you are only running it once. One options I can think of that you could do is only have the 1 instance and turn SingleInstance off, and the new window button could just run the script again (creating the same instance again). You can set the Run command to run the script again and again, and if you need parameters send to it you can do something like this

Code: Select all

Run %A_AHKPath% "Test.ahk" Par1 Par2 .....
But it also depends on what you plan on doing with your button settings. Other than that I don't have anything else in mind to improve it.
Thank you, this is something I'll try ASAP!
And I also need to understand the workings of the Run command :crazy:
The settings button will open another Gui which will read through a *.ini file with some parameters (colors, gui location, etc).
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: GUI duplicate script optimisation  Topic is solved

02 Aug 2018, 09:07

Salut KRG-23 and welcome to the AHK community,

Here's some suggestions:
- You can make scripts more reliable and easier to maintain using the lastfound window, when possible.
- GUI, New creates a new window and sets it as the default for the current thread, so that Gui, %Gui_Name% is redondant if you operate on the current thread's default GUI.
- Having said that, it is sometimes better to be explicit.
- You made a typo: WinGetPos, Xpos, Ypos,, %Gui_Name% should be WinGetPos, Xpos, Ypos,,, %Gui_Name% (or, ideally, WinGetPos, Xpos, Ypos - that is, omitting all four window parameters - if you use the lastfound window).
- Since the very first GUI is partially one-of-a-kind, you can use a Gosub in your auto-execute part to make the first GUI skip a step, step specific to others further GUI.

Code: Select all

#NoEnv
#SingleInstance force
#Warn

Gui_Number := 1
OffsetX := 50, OffsetY := 25
Xpos := 1000, Ypos := 100
MarginY := 10
; ===============================================
; Main Gui layout
; ===============================================
Gosub, Create_Window ; Jumps to the 'Create_Window' label and continues execution until Return is encountered
Gui, 1:Add, Button, -Tabstop ys wp hp gButton_Settings, Settings
Gui, 1:Show, AutoSize
return

Button_NewWindow: ; instructions préliminaires pour les fenêtres secondaires...
	; A GUI thread is defined as any thread launched as a result of a GUI action - as with 'Button_NewWindow'
	; Whenever a GUI thread is launched, that thread's last found window starts off as the GUI window itself > https://www.autohotkey.com/docs/commands/Gui.htm#DefaultWin
	WinGetPos, Xpos, Ypos ; all four window parameters are omiited - use the LastFoundWindow https://www.autohotkey.com/docs/misc/WinTitle.htm#LastFoundWindow
	; WinGetPos, Xpos, Ypos,, %Gui_Name%: ton utilisation de WinGetPos était erroné >>> WinGetPos, Xpos, Ypos,,, %Gui_Name%
	Xpos += OffsetX, Ypos += OffsetY
Create_Window:
	Gui, Margin,, 10
	Gui, %Gui_Number%:Default
	Gui, +Caption +Border +AlwaysOnTop
	Gui, Color, cFFFFFF
	Gui, Add, Text, xm ym, [Instance%Gui_Number%]
	Gui, Add, Button, Section xm w100 h25 gButton_NewWindow, New Window
	Gui, Add, Button, ys wp hp gGuiClose, &Close
	Gui, Show, x%Xpos% y%Ypos% AutoSize, Test
	Gui_Number++
return
	
GuiClose:
	if (A_GUI = 1) ; si c'est la main GUI...
		ExitApp
	; otherwise...
	PostMessage, 0x112, 0xF060 ; all four window parameters are omiited - use the LastFoundWindow https://www.autohotkey.com/docs/misc/WinTitle.htm#LastFoundWindow
return

Button_Settings:
return
my scripts
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: GUI duplicate script optimisation

02 Aug 2018, 09:18

So the way the run command is working is that the first part A_AHKPath (which is the path to the AHK folder) tells it was kind of script you will be running, in your case its an .ahk file. Since ahk runs based off of A_ScriptDir (which is the default) all you need is the script you want to run (if you decide you want to put a variable then you can just put the variable name without %% around it since its expecting a variable (which is why it has quotes). The final part is the parameters you want to launch the next script with, in your case you want to launch the script with a new index number. Since I assume you want to load a different ini file for each gui (which is why you are changing the number each time) then you can do something like this

Code: Select all

Button_NewWindow:
Gui_Name++
Run %A_AHKPath% "test.ahk" %Gui_Name%
Return
[code]
Then at the top of the script where you have your GUI settings add this
If (%1% = "")
Gui_Name := 1 ; to number Gui ids
Else
Gui_Name := %1%
This is untested, but the reason why I use the number 1 is because when you use the run command the 1 contains the Gui_Name that you sent. If that doesn't work then you can get the number out by using a loop like this

Code: Select all

Loop %0%
	Var%A_Index% := %A_Index%
%0% is the number of parameters you sent to the script so even if its 1 or 10000 the loop will run as many times as it needs.

Edit: A_AhkUser example is good if you get confused on the run command.
User avatar
KRG-23
Posts: 19
Joined: 07 Jun 2016, 05:45
Location: France

Re: GUI duplicate script optimisation

03 Aug 2018, 01:36

@MannyKSoSo: thank you for these more precise explanations and example. I'm already trying them.

@A_AhkUser: merci pour tes exemples! I'll also try them because obviously, my Gui is much more complex than the one I posted so I need to make sure the code adaptation also works. I was far from imagining that the typo I made would not make my script run smoothly as I did not get any errors. I'll also reflect on your optimisation and will come back to let you know if you guys solved my "problem".

Thank you both!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jameswrightesq, wpulford and 405 guests