Suggestion for form studio 2 (long)
Verfasst: Do 29. Dez 2022, 19:24
ISN AutoIt studio is an awesome piece of software. I learned it and loved it. It helps a lot in designing larger Autoit programs.
Yet there is always a little thing that bugs me. That is how to use the forms generated by Form Studio 2. For now, there are a few ways:
For example, suppose the form name is test.isf
1. Generate GUI and control creation code that can be placed inside a function.
2. Put the necessary #include in the main au3 file.
3. Other functions should be able to get the handles of the controls easily.
Yet there is always a little thing that bugs me. That is how to use the forms generated by Form Studio 2. For now, there are a few ways:
For example, suppose the form name is test.isf
- Use #include "test.isf" in the beginning.
We will get the following code:Pro:Code: Alles auswählen
#include <StaticConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Include <GuiButton.au3> $Test_Form = GUICreate("Test Form",350,350,-1,-1,-1,-1) $MyButton = GUICtrlCreateButton("My Text",120,120,100,30,-1,-1)
- Good for small programs which use one GUI
- Good for designing the Main GUI.
- Not good for Functions that dynamically create GUIs.
- This code cannot be enclosed within a function because of the multiple #include
- If we put it between functions, the control handle names might have duplication problems. Like the OK and Cancel buttons might have the same name.
- All GUIs and controls are declared and created when the program loads. It's very inefficient if some GUIs and controls are rarely used.
- Put GUI in a function, and return the handle to the GUI.
Pro:
Code: Alles auswählen
#include <StaticConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Include <GuiButton.au3> Func _Test_Form() $Test_Form = GUICreate("Test Form",350,350,-1,-1,-1,-1) $MyButton = GUICtrlCreateButton("My Text",120,120,100,30,-1,-1) return $Test_Form EndFunc
- Good to put the GUI creation along with other functions.
- The controls handles are enclosed, so they are safe to use inside that function.
- Good for event driven GUIs and controls.
- Exactly because control handles are enclosed in a function, it's not easy to use their handles in other functions.
- Need to put extra code in GUI or controls if something is not event-driven. Easy to forget those codes existence.
- Only put the control creation code in the .isf
We will get the following code:This is currently the way I do with all the forms. I just copy the GUI creation code before the form include, like below:Code: Alles auswählen
$MyButton = GUICtrlCreateButton("My Text",120,120,100,30,-1,-1)
Pro:Code: Alles auswählen
$Test_Form = GUICreate("Test Form",350,350,-1,-1,-1,-1) #include "test.isf"
- Good for putting into functions that needs to create a GUI by its own.
- Good for change of design of control layouts later on.
- Always need to copy the GUI creation line before the #include.
- If GUI setting/size is changed, it will not automatically reflect in the program. Need to remember to copy the GUI creation code.
- The main au3 file might not have the necessary #include
1. Generate GUI and control creation code that can be placed inside a function.
2. Put the necessary #include in the main au3 file.
3. Other functions should be able to get the handles of the controls easily.
- Proposal 1: Put all the GUI control creation code in a function. Add all the handles to a dictionary object, then return the dictionary object.
So after calling _Test_Form(), I will get an object that contains all the control handles, along with the GUI handle. I can use it easily in other functions:
Code: Alles auswählen
#include <StaticConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #Include <GuiButton.au3> Func _Test_Form() $oFunc = ObjCreate("Scripting.Dictionary") $Test_Form = GUICreate("Test Form",350,350,-1,-1,-1,-1) $oFunc.Add("GUI",$Test_Form) $MyButton = GUICtrlCreateButton("My Text",120,120,100,30,-1,-1) $oFunc.Add("MyButton", $MyButton ) return $oFunc EndFunc
Or even more directly:Code: Alles auswählen
$oTestForm = _TestForm() GUISetState( @SW_SHOW, $oTestForm.Item("GUI") ) GUICtrlSetData( $oTestForm.Item("MyButton"), "Other Text" )
This way all the GUI and controls are enclosed, and functions can still easily change them by using the dictionary object.Code: Alles auswählen
$oTestForm = _TestForm() GUISetState( @SW_SHOW, $oTestForm("GUI") ) GUICtrlSetData( $oTestForm("MyButton"), "Other Text" )
I prefer this way because it solves most of the problems.
I can put the #include "test.isf" in the beginning of an AU3 file, and let functions dynamically create the GUI on the fly, do something to the controls, wait for user interaction, then delete it. - Proposal 2: Generate the GUI and control code directly, but put the #include in the main au3 file.
This is easy for all the beginners, and easy for more advanced users too, but Form Studio must know which file is the main AU3 and be able to change it.