Saturday, June 15, 2013

Graphical User Interfaces (GUIs)

One of the newest features of AutoIT is its ability to create graphical user interfaces.
This feature adds an extraordinary amount of possibilities to the already useful
AutoIT language. Some of the most common uses of a GUI are installation menus,
input forms, and progress bars.
I am beginning this section with an example so you can see the layout of GUI creation
and get familiar with the functions used to create GUIs. Example 1 displays a two button
GUI with instructions and an image. It can be modified and used for anything
you can use a two-button chooser for: an installer for two different programs, a
chooser for two different types of users, etc. You can easily increase the size of the
GUI and create more buttons.

Example: Graphical User Interface - AI Smart Homes

; Includes the GuiConstants (required for GUI function usage)
#include <GuiConstants.au3>
; Hides tray icon
#NoTrayIcon
; Change to OnEvent mode
Opt('GUIOnEventMode', 1)
; GUI Creation
GuiCreate("Ai Smart Homes - Saint Louis, Missouri", 400, 300)
GuiSetIcon("icon.ico")
; Runs the GUIExit() function if the GUI is closed
GUISetOnEvent($GUI_EVENT_CLOSE, 'GUIExit')
; Logo / Pic
GuiCtrlCreatePic("logo.jpg",120,5,156,160)
; Instructions
GUICtrlCreateLabel("Please Choose an Option Below:", 50, 180, 300, 15, $SS_CENTER)
GUICtrlSetColor(−1,0xFF0000) ; Makes instructions Red
; Button1
GUICtrlCreateButton("Visit Our Website", 100, 210, 200, 30)
GUICtrlSetOnEvent(−1, 'website') ; Runs website() when pressed
; Button2
GUICtrlCreateButton("Send an Email", 100, 250, 200, 30)
GUICtrlSetOnEvent(−1, 'email') ; Runs email() when pressed
Func website()
; Hides the GUI while the function is running
GUISetState(@SW_HIDE)
Run("C:\Program Files\Internet Explorer\iexplore.exe www.aismarthomes.com")
Exit
EndFunc
Func email()
; Hides the GUI while the function is running
GUISetState(@SW_HIDE)
Run("mailto:contact@aismarthomes.com")
Exit
EndFunc
; Shows the GUI after the function completes
GUISetState(@SW_SHOW)
; Idles the script in an infinite loop - this MUST be included when using
OnEvent mode
While 1
Sleep(500)
WEnd
; This function makes the script exit when the GUI is closed
Func GUIExit()
Exit
EndFunc

FileInstall - Including Files in AutoIt Scripts

The FileInstall() function allows the inclusion of any file—such as an executable
or image file—in the compiled script executable. This is similar to #include, but
it dramatically increases the size of your compiled executable in most cases. This
is the syntax of the FileInstall() function:
FileInstall("sourcefile","destination" [,flag])
The flags for FileInstall() are optional. A flag of 0 tells the function not to overwrite
existing files. Use a flag of 1 if you would like to overwrite any existing files
the script may encounter. The source file cannot be a variable; it must be a string,
and it cannot contain wildcards.
Example 1 is an installation you can perform with the FileInstall() function that
extracts all installation files to the temp directory. When compiled, the entire installation
is a single executable.

Example 1. Using the FileInstall() Function
#NoTrayIcon
Opt("MustDeclareVars", 1)
FileInstall("C:\Documents and Settings\Administrator\Desktop\Program\" & _
"Setup.exe", @TempDir & "\Setup.exe", 1)
FileInstall("C:\Documents and Settings\Administrator\Desktop\Program\" & _
"Setup.exe", @TempDir & "\setup.ico", 1)
FileInstall("C:\Documents and Settings\Administrator\Desktop\Program\" & _
"Setup.exe", @TempDir & "\setup.ini", 1)
FileInstall("C:\Documents and Settings\Administrator\Desktop\Program\" & _
"Setup.exe", @TempDir & "\program.dll", 1)
FileInstall("C:\Documents and Settings\Administrator\Desktop\Program\" & _
"Setup.exe", @TempDir & "\readme.txt", 1)
Run(@TempDir & "\Setup.exe")
WinWait("Installation Wizard", "Welcome to the")
If Not WinActive("Installation Wizard", "Welcome to the") Then _
WinActivate("Installation Wizard", "Welcome to the")
WinWaitActive("Installation Wizard", "Welcome to the")
ControlClick("Installation Wizard", "", "Next")
WinWait("Installation Wizard", "Installation Complete")
If Not WinActive("Installation Wizard", "Installation Complete") Then _
WinActivate("Installation Wizard", "Installation Complete")
WinWaitActive("Installation Wizard", "Installation Complete")
ControlClick("Installation Wizard", "", "Finish")


In this example, FileInstall() copies five files to the temp directory, then the Run
() command runs Setup.exe. The program then waits for the installation wizard to
appear using WinWait() and makes it active using WinActivate() before clicking
Next with ControlClick() and then Finish with ControlClick() to complete the
installation.