AutoIt GUI Tutorial

This beginers AutoIt GUI Tutorial is the first in a series of Tutorials focused on GUI creation.

Next: How to Disable and Enable GUI Elements

AutoIt Logo

This Tutorial will get you started on GUI creation in AutoIt. This tutorial shows how to create a simple GUI with two buttons, and how to perform a specific task depending on which button was pressed.

We will just briefly explain the top part of the script, that is the part outside the main function.

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

MainGUI()

The GUIConstantsEx.au3 file that you will see included in the top of the script, contains all the system events, such as the event of: maximizing, minimizing or closing a window.

The MustDeclareVars option makes it so that variables must be declared with either: Dim, Local or Global before use.

Finally the MainGUI custom function call just runs the MainGUI function that you will find further down in the script.

The MainGUI Function

As mentioned earlier in the tutorial, the MustDeclareVars option makes it so that we must declare variables before we can use them. Inside the MainGUI function, Local was used. Variables declared using local are only available at the local scope - in this case inside the function where they are declared.

Local $Button1, $Button2, $msg

The GUICreate function creates the GUI, assigns a title, and optionally controls the style of the GUI window. This function will be covered in a later Tutorial.

GUICreate("My GUI Window Title")

The GUICoordMode controls the coordinate mode to be used when placing new GUI elements, in this case the two buttons. I.e. Absolute or relative. This will also be covered in a later tutorial.

Opt("GUICoordMode", 2)

The GUICtrlCreateButton function is used to create buttons. This will also be covered in a later tutorial.

$Button1 = GUICtrlCreateButton("Button 1", 10, 30, 100)
$Button2 = GUICtrlCreateButton("Button 2", 0, -1)

In this example, the GUISetState function is used to show a newly created window.

GUISetState()

The while loop simply performs certain select statement checks continuously - in this example, the window only closes if $GUI_EVENT_CLOSE was performed.

While 1
  $msg = GUIGetMsg()
  Select
   Case $msg = $GUI_EVENT_CLOSE
     ExitLoop
   Case $msg = $Button1
     MsgBox(0, 'Button 1', 'Button 1 was pressed')
   Case $msg = $Button2
     MsgBox(0, 'Button 2', 'Button 2 was pressed')
  EndSelect
WEnd

AutoIt GUI Example

The full GUI script can be seen below:

#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)

MainGUI()

Func MainGUI()
  Local $Button1, $Button2, $msg
  GUICreate("My GUI Window Title")

  Opt("GUICoordMode", 2)
  $Button1 = GUICtrlCreateButton("Button 1", 10, 30, 100)
  $Button2 = GUICtrlCreateButton("Button 2", 0, -1)

  GUISetState()

  ; Run the GUI until the window is closed
  While 1
    $msg = GUIGetMsg()
    Select
     Case $msg = $GUI_EVENT_CLOSE
       ExitLoop
     Case $msg = $Button1
       MsgBox(0, 'Button 1', 'Button 1 was pressed')
     Case $msg = $Button2
       MsgBox(0, 'Button 2', 'Button 2 was pressed')
    EndSelect
  WEnd
EndFunc