Dynamic GUI using variadic command lines

Post your working scripts, libraries and tools
User avatar
DrReflex
Posts: 15
Joined: 25 May 2015, 02:57

Dynamic GUI using variadic command lines

21 Jul 2019, 10:12

I needed a way to dynamically change several GUIs. I wanted to make the changes programmatically or through user input. I found a way to do this using variadic GUI command line parameters and a GUI building function BuildGui(). BuildGui() that accepts a variadic list of GUI, command line options.

Using BuildGui() you can:
1. Load a GUI from a file
2. Load a GUI from program code using a format similar to current GUI, command lines
3. Modify GUIs based upon program or user input
4. Easily build arrays of various control types

Enjoy.

Refinements and improvements appreciated.

Code: Select all

; DynamicGUIExamples.ahk
; AutoHotkey Version: 1.1.30.03
{ ; MAIN PROGRAM
	#NoEnv 
	#SingleInstance, Force	
	SendMode Input  			
	SetWorkingDir %A_ScriptDir%	
	
  	global DoVersionA := true
	
  	;***** 3 EXAMPLES OF INCREASING COMPLEXITY ****
	;EXAMPLE 1: Non dynamic use of BuildGui()
	{
		BuildGui("MyGui1","New","","EXAMPLE 1 - New")
		BuildGui("MyGui1","+Border +HwndMyGui9 -SysMenu")
		BuildGui("MyGui1","Add","Text","","EXAMPLE 1 - MyGui1:New Title REPLACED BY MyGUI1:Show Title")
		BuildGui("MyGui1","Show","x20 y100 w400 h50 NoActivate","EXAMPLE ` - Show")
	}

	;EXAMPLE 2: Dynamic use of BuildGUi() using parameter lists.  Could be loaded from a file, by your program, or from the keyboard
	{
		Params1 := ["MyGui2","New","","EXAMPLE 2 - New"]
		Params2 := ["MyGui2","+Border +HwndMyGui9 -SysMenu"]
		Params3 := ["MyGui2","Add","Text","","EXAMPLE 2 - MyGui2:New Title REPLACED BY MyGUI2:Show Title"]
		Params4 := ["MyGui2","Show","x500 y100 w400 h50 NoActivate","EXAMPLE ` - Show"]
		
		BuildGui(Params1*)
		BuildGui(Params2*)
		BuildGui(Params3*)
		BuildGui(Params4*)
	}
	
	;EXAMPLE 3: Dynamic use of BuildGUI() 
	Example3Gui()

	return
} ; END MAIN PROGRAM

Example3Gui()
{
	;CONSTANTS
	xGui 			:= 20
	yGui 			:= 220
	wGui 			:= 400
	hGui 			:= 260
	xMargin			:= 10
	yMargin			:= 10
	xText			:= xMargin
	yText			:= YMargin
	wText	 		:= wGui - (2 * XMargin)
	hText	  		:= 20
	RowsButtons		:= 2
	ColumnsButtons 	:= 2
	wButton			:= 100
	hButton			:= 100
	
	MyGui			:= "MyGui3"
	
	;BUILD MyParamsNew
	{
		MyOptionsNew	:= ""
		MyTitleNew	:= "DYNAMGIC GUI - VERSION 1"
		MyParamsNew	:= [MyGui,"New",MyOptionsNew,MyTitleNew]
	}
	
	;BUILD MyParamsMenu
	{
		MyMenuName	:= "MyMenuBar"
		MyParamsMenu	:= [MyGui,"Menu",MyMenuName]
	}
	
	;BUILD MyParamsText
	{
	MyOptionsText	:= "x" . xMargin . " y" . yMargin
	MyTextText	:= "EXAMPLE 3 - MyGui3:New TITLE REPLACED  BY MyGui3:Show TITLE"	
	MyParamsText	:= [MyGui,"Add","Text",MyOptionsText,MyTextText]
	}
	
	;BUILD MyParamsButtons[1..4], MyParamsEdit, MyParamsShow (GUI VERSIONS A AND B)  ################ 
	;##### DYNAMICALLY CHANGE THE GUI  #####
	;  NOTE: COULD CHANGE ENTIRE GUI.
	{
		;**********************
		;***** VERSION A ******
		;**********************
		If (DoVersionA)
		{
			;BUILD MyParamsButton[1..4] (VERSION A)
			{
				MyParamsButton := {}
				Menu, MyMenuBar, Add, ChangeToVersion

				MyMsg := "`nRows:" . RowsButtons . "    "	
				MyMsg .= "Columns:" . ColumnsButtons . "`n"
				Loop, %RowsButtons%
				{
					Row := A_Index
					Loop, %ColumnsButtons%
					{
						Column		:= A_Index			
						xButton		:= ((Column - 1) * (wButton + xMargin)) + xMargin
						yButton		:= ((Row     -1) * (hButton + yMargin)) + (2 * yMargin) + hText
						OptionsButton	:= "x" . xButton . " y" . yButton . " w" . wButton . " h" . hButton			
						NumberButton	:= ((Row - 1) * ColumnsButtons) + Column
						TextButton	:= "Button" . NumberButton
						MyParamsButton[NumberButton] := [MyGui,"Add","Button",OptionsButton,TextButton]
					}
				}
			}
			
			;BUILD MyParamsEdit (VERSION A)
			{
				xEdit		:= (wButton * 2) + (xMargin * 3)
				yEdit		:= hText + (yMargin *2)
				wEdit		:= wGui - xEdit - xMargin
				hEdit		:= hGui - yEdit - yMargin
				MyOptionsEdit	:= "x" . xEdit " y" . yEdit . " w" . wEdit . " h" . hEdit
				MyTextEdit	:= "DYNAMIC GUI`nVERSION A`n`nVersion A text to edit.`n`n`nNOTE: CLICK MENU [ChangeToVersion] TO SEE VERSION B."
				MyParamsEdit	:= [MyGui,"Add","Edit",MyOptionsEdit,MyTextEdit]
			}
			
			;BUILD MyParamsShow
			{
				xShow 		:= xGui	
				yShow 		:= yGui
				wShow 		:= wGui
				hShow 		:= hGui
				MyOptionsShow	:= "x" . xShow " y" . yShow . " w" . wShow . " h" . hShow . " NoActivate"
				MyTitleShow	:= "DYNAMIC GUI - VERSION A"
				MyParamsShow	:= [MyGui,"Show",MyOptionsShow,MyTitleShow]
			}
		} else  ;DoVerionB
		;**********************
		;***** VERSION B ******
		;**********************			
		{
			;BUILD MyParamsEdit (VERSION B)
			{
				xEdit		:= xMargin		
				yEdit		:= hText + (yMargin *2)
				wEdit		:= wGui - xEdit - (wButton * 2) - (xMargin * 3)
				hEdit		:= hGui - yEdit - yMargin
				MyOptionsEdit	:= "x" . xEdit " y" . yEdit . " w" . wEdit . " h" . hEdit
				MyTextEdit	:= "DYNAMIC GUI`nVERSION B`n`nVersion B text to edit.`n`n`nNOTE: CLICK MENU [ChangeToVersion] TO SEE VERSION A."
				MyParamsEdit	:= [MyGui,"Add","Edit",MyOptionsEdit,MyTextEdit]
			}
			
			;BUILD MyParamsButton[1..4] (VERSION B)
			{
				MyParamsButton := {}
				Menu, MyMenuBar, Add, ChangeToVersion

				Loop, %RowsButtons%
				{
					Row := A_Index
					Loop, %ColumnsButtons%
					{
						Column		:= A_Index				
						xButton		:= ((Column - 1) * (wButton + xMargin)) + wEdit + (xMargin * 2)
						yButton		:= ((Row     -1) * (hButton + yMargin)) + (2 * yMargin) + hText ;(+hMenu)
						OptionsButton	:= "x" . xButton . " y" . yButton . " w" . wButton . " h" . hButton
						NumberButton	:= ((Row - 1) * ColumnsButtons) + Column
						TextButton	:= "Button" . NumberButton
						MyParamsButton[NumberButton] := [MyGui,"Add","Button",OptionsButton,TextButton]
					}
				}
			}
			
			;BUILD MyParamsShow
			{
				xShow		:= xGui	
				yShow		:= yGui
				wShow		:= wGui
				hShow		:= hGui
				MyOptionsShow	:= "x" . xShow " y" . yShow . " w" . wShow . " h" . hShow . " NoActivate"
				MyTitleShow	:= "DYNAMIC GUI - VERSION B"
				MyParamsShow	:= [MyGui,"Show",MyOptionsShow,MyTitleShow]
			}
		}			
	}
	
	;BUILD GUI USING BuildGui()
	{
		BuildGui(MyParamsNew*)
		BuildGui(MyParamsMenu*)
		BuildGui(MyParamsText*)
		Loop, %RowsButtons%
		{
			Row := A_Index
			Loop, %ColumnsButtons% 
			{
				Column := A_Index
				NumberButton := ((Row - 1) * ColumnsButtons) + Column
				ParamsButton := MyParamsButton[NumberButton]
				BuildGui(ParamsButton*)
			}
		}
		BuildGui(MyParamsEdit*)
		BuildGui(MyParamsShow*)	
		return
	}
}

BuildGui(BuildParams*)
{
	OptionsFirstCharList	:= "-,+"
	SubCommandList1 	:= "Add"
	SubCommandList2 	:= "Color,Font,Margin,New,Show"
	SubCommandList3 	:= "Flash,Menu,Submit"
	SubCommandList4 	:= "Cancel,Default,Destroy,Hide,Maximize,Minimize,Restore"
	
	BuildGui 			:= BuildParams[1]
	BuildSubCommand 	:= BuildParams[2]
	
	;CHECK TO SEE IF BuildGui IS NULL.  SET BoulGuiName ACCORDINGLY.  USED IN EACH PROCESS SECTION
	If (BuildGui == "")
	{
		BoulGuiName := false
	} else
	{
		BoulGuiName := true
	}

	;***** PROCESS VARIOUS SUBCOMMANDS *****
	;PROCESS BuildSubCommand = +/-Option1 +/-Option2 ... OR
	SubStrFirstChar := SubStr(BuildSubCommand,1,1)  
	If SubStrFirstChar in %OptionsFirstCharList%
	{
		If (BooGuiName == true)
		{
			Gui, %BuildGui%:%BuildSubCommand%
		} else
		{
			Gui, %BuildSubCommand%
		}
		return
	}

	;PROCESS BuildSubCommand = Add
	if (BuildSubCommand == "Add")
	{
		BuildControlType := BuildParams[3]
		BuildOptions 	 := BuildParams[4]
		BuildText       	 := BuildParams[5]
		If (BooGuiName == true)
		{
			Gui, %BuildGui%:%BuildSubCommand%,%BuildControlType%,%BuildOptions%,%BuildText%
		} else
		{
			Gui, %BuildSubCommand%,%BuildControlType%,%BuildOptions%,%BuildText%
		}
		return
	}
		
	;PROCESS BuildSubCommand = "Color", "Font", "Margin", "New", or "Show" 
	If BuildSubCommand in %SubCommandList2% 
	{
		BuildParam3 := BuildParams[3]
		BuildParam4 := BuildParams[4]
		If (BoolGuiName == true)
		{
			Gui, %BuildGui%:%BuildSubCommand%,%BuildParam3%,%BuildParam4%
		} else
		{
			Gui, %BuildSubCommand%,%BuildParam3%,%BuildParam4%
		}
		return
	}

	;PROCESS BuildSubCommand = "Flash", "Menu", or "Submit"
	If BuildSubCommand in %SubCommandList3% 
	{
		BuildParam3 := BuildParams[3]
		If (BoolGuiName == true)
		{
			Gui, %BuildGui%:%BuildSubCommand%,%BuildParam3%
		} else
		{
			Gui, %BuildSubCommand%,%BuildParam3%
		}
		return
	}

	;PROCESS BuildSubCommand = "Cancel", "Default", "Destroy", "Hide", "Maximize", "Minimize", and "Restore"
	If BuildSubCommand in %SubCommandList4% 
	{
		If (BoolGuiName == true)
		{
			Gui, %BuildGui%:%BuildSubCommand%
		} else
		{
			Gui, %BuildSubCommand%
		}
		return
	}
  return 
}

ChangeToVersion:
{
	DoVersionA := !DoVersionA
	Example3Gui()
	return
}

Esc::
GUIClose:
ExitApp

Return to “Scripts and Functions”

Who is online

Users browsing this forum: ahk7 and 49 guests