Page 1 of 1

GUI() function replaces GUI command line.

Posted: 21 Jul 2019, 10:12
by DrReflex
GUI() FUNCTION:

I needed a way to dynamically change several GUIs. I wanted to make the changes programmatically (or through user input). An efficient way to accomplish this was to replace the GUI statement with the variadic function GUI(). I can now programmatically build (or specify) a list of GUI command line options, send them to GUI() as a variadic parameter list, and have GUI() process the list and execute the appropriate GUI, command with the specified options.

Using Gui() 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

You pass the GUI command line parameters just like you would using the GUI command changing GuiName:SubCommand to GuiName,SubCommand
GUI COMMAND FORMAT: Gui, GuiName:SubCommand, Value1, Value2, Value3
Gui() FUNCTION FORMAT: Gui(GuiName, SubCommand, Value1, Value2, Value3)

PLEASE TAKE A LOOK AT THE EXAMPLES BELOW.

Code: Select all

; DynamicGUIExamples.ahk  - USING GUI()
; 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 Gui().  Function equivalent to GUI command line.
	{
		Gui("MyGui1","New","","EXAMPLE 1 - New")
		Gui("MyGui1","+Border +HwndMyGui9 -SysMenu")
		Gui("MyGui1","Add","Text","","ORIGINAL TITLE: 'EXAMPLE 1 - New' REPLACED BY: 'EXAMPLE 1 - Show'")
		Gui("MyGui1","Show","x20 y100 w400 h50 NoActivate","EXAMPLE 1 - Show")
	}

	;EXAMPLE 2: Dynamic use of GUi() using parameter lists.  These 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","","ORIGINAL TITLE: 'EXAMPLE 2 - New' REPLACED BY: 'EXAMPLE 2 - Show'"]
		Params4 := ["MyGui2","Show","x500 y100 w400 h50 NoActivate","EXAMPLE 2 - Show"]
		
		Gui(Params1*)
		Gui(Params2*)
		Gui(Params3*)
		Gui(Params4*)
	}
	
	;EXAMPLE 3: Dynamic use of GUI() to programmatically change a GUI (includes a programmatically generated array of buttons).
	Example3Gui()

	return
} ; END MAIN PROGRAM

Example3Gui()  ;Example3Gui(): Dynamic use of GUI() to programmatically change a GUI (includes a programmatically generated array of buttons).
{
	;CONSTANTS
	xGui 			:= 20
	yGui 			:= 220
	wGui 			:= 500
	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 - ""CLICK MENU ITEM ChangeToVersion TO SWITCH BETWEEN VERSIONS A AND B"""	
	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:`n1.  CLICK MENU ITEM [ChangeToVersion]`n  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:`n1.  CLICK MENU [ChangeToVersion]`n  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 Gui()
	{
		Gui(MyParamsNew*)
		Gui(MyParamsMenu*)
		Gui(MyParamsText*)
		Loop, %RowsButtons%
		{
			Row := A_Index
			Loop, %ColumnsButtons% 
			{
				Column := A_Index
				NumberButton := ((Row - 1) * ColumnsButtons) + Column
				ParamsButton := MyParamsButton[NumberButton]
				Gui(ParamsButton*)
			}
		}
		Gui(MyParamsEdit*)
		Gui(MyParamsShow*)	
		return
	}
}

Gui(BuildParams*)
{
;GUI   COMMAND FORMAT:  Gui, GuiName:SubCommand, Value1, Value2, Value3
;Gui() FUNCTION FORMAT: Gui(GuiName, SubCommand, Value1, Value2, Value3)

	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
HERE IS JUST THE Gui() FUNCTION:

Code: Select all

Gui(BuildParams*)
{
;GUI   COMMAND FORMAT:  Gui, GuiName:SubCommand, Value1, Value2, Value3
;Gui() FUNCTION FORMAT: Gui(GuiName, SubCommand, Value1, Value2, Value3)

	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 
}
Enjoy.

Refinements and improvements appreciated.

UPDATED 09/21/19
NOTE: Similar Menu() Function to follow.