GuiButtons to use the same "function"/gLabel. Store values within the buttons and use the same gLabel

Helpful script writing tricks and HowTo's
User avatar
JoakimU
Posts: 12
Joined: 07 Apr 2021, 09:45

GuiButtons to use the same "function"/gLabel. Store values within the buttons and use the same gLabel

Post by JoakimU » 07 Apr 2021, 10:09

Hi. This may be old stuff but for those who have Guis with a lot of buttons, text, ddls etc and this example might help you minimize A lot of labels.
I have a script with 36 buttons (An app for a MIDI controller keyboard). Narrowing it down to use only one gButton label really minimizes hundeds of rows of code...

This example has 5 buttons,
  • each with 3 different "variables" in it! the vID is an A_GuiControl that can be used as values to process in the gLabel.
  • the buttons are of the same type so they can go to the same gLabel!
the v -value in each button is split into 3 different variables and can then be used in a function:

Code: Select all

#SingleInstance Force
SetWorkingDir, %A_ScriptDir%
Gui, 1: add, text,X10 y9 ,Several Buttons - using same subroutine:
Gui, 1: add, Button, w60 x10  y+10 vfirst_1_FF7777  gClickButton, Button A
Gui, 1: add, Button, w60 x+10 yp+0 vsecond_2_77FF77 gClickButton, Button B
Gui, 1: add, Button, w60 x+10 yp+0 vthird_3_B0E0FF  gClickButton, Button C
Gui, 1: add, Button, w60 x+10 yp+0 vfourth_4_F088FF gClickButton, Button D
Gui, 1: add, Button, w60 x+10 yp+0 vfifth_5_F0F070  gClickButton, Button E
Gui, 1: Add, Progress, Background202020 x10 y+8 w340 h20  cFFFFFF  vColorBox, 100
Gui, 1: Add, Text, BackgroundTrans x10 yp+3 w340 h20 center vTextinfo, Press a button
Gui,1: Show, , ButtonsSubroutine
Return

ClickButton:
{ ; Same subroutine for each button. The v-ID (A_GuiControl) will be split into 3 useful variables.
Value := StrSplit(A_GuiControl, "_")
	set_label := Value[1]
	set_value := Value[2]
	set_color := Value[3]
; Now use those Global variables. For learning purposes; Call a function:
CalcFunction(set_label,set_value,set_color)
Return
}

CalcFunction(_Name_,_value_,_color_)
{ ; This is a simple function with 3 local variables.
GuiControl, +c%_color_%, ColorBox, 100 ; set_color on the 100% filled progress bar
GuiControl, , Textinfo, The %_Name_% button: (%_value_%) was pressed. ; Update the Gui text 
Return
}

GuiClose:
Guiescape:
exitapp
WatsonEnterprises
Posts: 19
Joined: 25 May 2020, 23:04

Re: GuiButtons to use the same "function"/gLabel. Store values within the buttons and use the same gLabel

Post by WatsonEnterprises » 26 May 2021, 13:40

That's kind of clever, I hadn't thought of that. But AHK does support sending parameters to a glabel without smuggling them inside the variable's name. You can assign a bound function object to the buttons using GuiControl +g. Adds a few lines, but makes your intent more obvious.

Code: Select all

#SingleInstance Force
SetWorkingDir, %A_ScriptDir%
Gui, 1: add, text,X10 y9 ,Several Buttons - using same subroutine:
Gui, 1: add, Button, w60 x10  y+10 vMyButton1, Button A
Gui, 1: add, Button, w60 x+10 yp+0 vMyButton2, Button B
Gui, 1: add, Button, w60 x+10 yp+0 vMyButton3, Button C
Gui, 1: add, Button, w60 x+10 yp+0 vMyButton4, Button D
Gui, 1: add, Button, w60 x+10 yp+0 vMyButton5, Button E

ARGS_FOR_ALL_BUTTONS := [ {name:"first", value:1, color:"FF7777"}
						, {name:"second", value:2, color:"77FF77"}
						, {name:"third", value:3, color:"B0E0FF"}
						, {name:"fourth", value:4, color:"F088FF"}
						, {name:"fifth", value:5, color:"F0F070"} ]

for buttonNumber, argsForThisButton in ARGS_FOR_ALL_BUTTONS {
	fnForButton := Func("CalcFunction").Bind(argsForThisButton.name, argsForThisButton.value, argsForThisButton.color)
	GuiControl +g, % ("MyButton" buttonNumber), % fnForButton
}

Gui, 1: Add, Progress, Background202020 x10 y+8 w340 h20  cFFFFFF  vColorBox, 100
Gui, 1: Add, Text, BackgroundTrans x10 yp+3 w340 h20 center vTextinfo, Press a button
Gui,1: Show, , ButtonsSubroutine
Return


CalcFunction(_Name_,_value_,_color_)
{ ; This is a simple function with 3 local variables.
GuiControl, +c%_color_%, ColorBox, 100 ; set_color on the 100% filled progress bar
GuiControl, , Textinfo, The %_Name_% button: (%_value_%) was pressed. ; Update the Gui text 
Return
}

GuiClose:
Guiescape:
exitapp

garry
Posts: 3736
Joined: 22 Dec 2013, 12:50

Re: GuiButtons to use the same "function"/gLabel. Store values within the buttons and use the same gLabel

Post by garry » 27 May 2021, 02:16

thank you for the examples
something old basic , see buttonname and start url or program

Code: Select all

;-- Example GUI with Buttons to Start URL/Program -----------
#warn
#NoEnv
name1=TEST
setworkingdir,%a_scriptdir%
Gui,2:default
Gui,2: -DPIScale
SS_REALSIZECONTROL := 0x40
transform,s,chr,127
WA:=A_screenwidth,HA:=A_screenheight,xx:=100
;Gui,2:Color,C8D0D4,Black                  ;-gray
Gui,2:Color,Black,Black
;- maybe change fontsize don't know which is the best font-size depending screen-size
;============ GUISIZEx DPIx 4Kx 3840*2160 is 100% ============
if (wa=3840)
 Gui,2:Font,s12 cBlack,Lucida Console
;============ GUISIZEx DPIx FHD 1920*1080 is 100% ============
else if (wa=1920)
 Gui,2:Font,s10 cBlack,Lucida Console
else
 Gui,2:Font,s7 cBlack,Lucida Console
;=============================================================
;--------------------
cx:=""
ck:=""
sectx=   
(Ltrim comments Join`r`n % 
Autohotkey;https://www.autohotkey.com/
YT_Rotterdam2Amsterdam;https://www.youtube.com/watch?v=HfPCdJapIXA
YT_Offenbach_Barcarole;https://www.youtube.com/watch?v=R-MbbebSQjQ&t=91
NL_Rijkswaterstaat;https://www.youtube.com/channel/UCOESCgenMD6CMDiiHe-iG-g
NL_Archive_1;https://www.youtube.com/channel/UC3Xw6AMOJIiUJ0cNHB7CdIg
NL_Archive_2;https://basromeijnfilms.blogspot.com/
British_pathe;https://www.youtube.com/channel/UCGp4u0WHLsK8OAxnvwiTyhA
BBC_Archive;https://www.bbc.co.uk/archive/
LA_CAMERA_STYLO;https://www.youtube.com/channel/UCxT4JLBXafKV7dCafIceCUg/videos  ;- Film collection
News_Portugal;https://news.google.com/topstories?hl=pt-PT&gl=PT&ceid=PT:pt-150
Weather_Portugal;https://www.timeanddate.com/weather/portugal/lisbon
Hurrican;https://www.accuweather.com/en/hurricane/tracker
Typhoon;https://www.metoc.navy.mil/jtwc/jtwc.html
Wind_Asia;https://earth.nullschool.net/#current/wind/surface/level/orthographic=123.04,23.45,1821/loc=124.879,13.692
Euronews;https://de.euronews.com/bulletin
Charmap;charmap
Modern_Times_1936;https://www.youtube.com/watch?v=2gLa4wAia9g     ;- Full movie Charles Chaplin
)
;---------------------
loop,parse,sectx,`n,`r
  {
  x:=a_loopfield
  if x=
     break
  stringsplit,k,x,`;
  cx .= k1 . s
  ck .= k2 . s
  }
stringsplit,m,ck,%s%
stringsplit,q,cx,%s%
totx:=(q0-1)
x:=(wa*1)/xx,y:=(ha*2)/xx,w :=(wa*13 )/xx,h:=(ha*2.5)/xx,v:=(ha*.5)/xx
Gui,2:add,text,section x%x% y%y% w0 h0,  ;- button position
i=0
Loop,%totx%
  {
  i++
  if (i=10)
    {
    x:=(wa*15)/xx
    Gui,2:add,text,section x%x% y%y% w0 h0,     ;- new button position
    }
  BTName:= q%i%
  Gui,2: Add, Button, xs   y+%v% w%w%  h%h% gStart1 v%btname%_%i% ,%btname%
  }
x:=(wa*1)/xx,y:=(ha*1)/xx,w:=(wa*30)/xx,h:=(ha*30)/xx
Gui,2:Show, x%x% y%y% h%h% w%w%,%name1%
return
;-------------------------------------------------------------------------
2Guiclose:
exitapp
;-------------------------------------------------------------------------
;--------------- start --------------------------
start1:
gui,2:submit,nohide
Mousegetpos,,,,Ctrl       ;- Button1
r:= a_guicontrol          ;- vVar ( Buttonname )
stringtrimleft,ct,ctrl,6  ;- Button-(1)
url:= m%ct%
;msgbox, 262208, ,R=%r%`nCT=%ct%`nCTRL=%ctrl%`nURL=%url%
try
 run,%url%
return
;=================== END SCRIPT =============================================
Post Reply

Return to “Tutorials (v1)”