• No results found

You have just created your GUI, and you want to do something, when some clicks a button, or you want to add text into your edit box when someone clicks on a picture. To do this and more, we must receive input from our GUI. For this section I will be using this as my GUI:

;Required Includes.

#include <GUIConstantsEx.au3> ;Constants for GUI Events

#include <EditConstants.au3> ;Edit constants. Required for the styles we used.

;Declare any variables.

;Create the GUI

$hGUI = GUICreate ("Learning to script with AutoIt V3- Example GUI", 400, 300)

;Create a lable

;Below you can see & _. It allows us to split up lines, making it easier to read.

$hLabel = GUICtrlCreateLabel ("This is a label control. Type text into the Input control" & _ "and press the button to set the text of the edit control. " & _

"Type /SPECIAL in the edit for a special message!",10, 10, 380, 40)

;Create an input control

LEARNING TO SCRIPT WITH AUTOIT V3 Page 45

$hInput = GUICtrlCreateInput ("This is an Input Control. Type stuff here!", 10, 50, 380, 20)

;Create an edit control

$hEdit = GUICtrlCreateEdit ("This is the edit control. We used a style to make it multiline and read-only!!", 10, 80, 380, 170, BitOR ($ES_MULTILINE, $ES_READONLY))

;Create the button

$hButton = GUICtrlCreateButton ("Press me!", 320, 260, 70, 25)

;Show the GUI. We need this line, or our GUI will NOT be displayed!

GUISetState (@SW_SHOW)

As I run through each step on getting input you may also see how I read the controls using GUICtrlRead. It is how we read data of most controls, other than some controls (Radios + Checkboxes) which we don’t cover in this tutorial. You may also notice the #Include lines. The comments explain what they are for.

There are two ways we can check for messages- A GUI Message loop or OnEvent messages. We will start with the GUI Message Loop.

9.3.1 Message Loop.

This should be incased in a loop and then checked with conditional statement. In this example we will use a Switch Statement. See the commented code below to see how we’ve done everything.

This code should be placed at the end of our initial GUI Code.

;Endless While loop While 1

$nMsg = GUIGetMsg () Switch $nMsg

Case $GUI_EVENT_CLOSE; The red X is pressed Exit ;Exit the script

Case $hLabel ;The label

MsgBox (0, "Hello!", "We have clicked on the label!"); Say Hello Case $hButton ;The Button

$read = GUICtrlRead ($hInput)

;Check to see if we have /SPECIAL using StringInStr.

If StringInStr ($read, "/SPECIAL") Then

;We have it, display the message.

MsgBox (0, "WOW!", "This is a special message!") Else

;Get Existing Data of edit

$read2 = GUICtrlRead ($hEdit)

$text = $read2 & @CRLF & $read ; Join the existing and the new text seperated by a line.

GUICtrlSetData ($hEdit, $text) ; Set the edit control to have our new data!

GUICtrlSetData ($hInput, "");Reset the data of the input.

EndIf EndSwitch

Wend

LEARNING TO SCRIPT WITH AUTOIT V3 Page 46 9.3.2 On event Mode

To use OnEvent mode we must add the following line after we have our includes, so that AutoIt knows that we are using OnEvent mode.

Opt("GUIOnEventMode", 1)

Once we have done that, we have to tell AutoIt what functions to call when it receives a message from our controls. We will do that before GUISetState(@SW_SHOW)

GUICtrlSetOnEvent($hLabel, "LabelFunction") ; Set the label control's function GUICtrlSetOnEvent($hButton, "ButtonFunction") ; The button's function

GUISetOnEvent($GUI_EVENT_CLOSE, "ExitGUI") ; What function to call when we try close the GUI

We also have to have a While loop to keep the GUI open, but with a sleep to keep CPU usage down.

;Endless While loop to keep the GUI Open While 1

Sleep(10); So we don't use heaps of CPU Wend

We also have to create the functions, so here they are.

Func LabelFunction()

MsgBox(0, "Hello!", "We have clicked on the label!"); Say Hello EndFunc ;==>LabelFunction

Func ButtonFunction()

$read = GUICtrlRead($hInput)

;Check to see if we have /SPECIAL using StringInStr.

If StringInStr($read, "/SPECIAL") Then

;We have it, display the message.

MsgBox(0, "WOW!", "This is a special message!") Else

;Get Existing Data of edit

$read2 = GUICtrlRead($hEdit)

$text = $read2 & @CRLF & $read ; Join the existing and the new text seperated by a line.

GUICtrlSetData($hEdit, $text) ; Set the edit control to have our new data!

GUICtrlSetData($hInput, "");Reset the data of the input.

EndIf

EndFunc ;==>ButtonFunction Func ExitGui ()

Exit ; Exit the program EndFunc

If all was done correctly you should have a script that works just like the Message loop. If not here is the full code.

;Required Includes.

LEARNING TO SCRIPT WITH AUTOIT V3 Page 47

#include <GUIConstantsEx.au3> ;Constants for GUI Events

#include <EditConstants.au3> ;Edit constants. Required for the styles we used.

;Declare any variables/opts.

Opt("GUIOnEventMode", 1);We need this otherwise our GUI will not be OnEvent Mode.

;Create the GUI

$hGUI = GUICreate("Learning to script with AutoIt V3- Example GUI", 400, 300)

;Create a lable

;Below you can see & _. It allows us to split up lines, making it easier to read.

$hLabel = GUICtrlCreateLabel("This is a label control. Type text into the Input control" & _

"and press the button to set the text of the edit control. " & _

"Type /SPECIAL in the edit for a special message!", 10, 10, 380, 40)

;Create an input control

$hInput = GUICtrlCreateInput("This is an Input Control. Type stuff here!", 10, 50, 380, 20)

;Create an edit control

$hEdit = GUICtrlCreateEdit("This is the edit control. We used a style to make it multiline and read-only!!", 10, 80, 380, 170, BitOR($ES_MULTILINE, $ES_READONLY))

;Create the button

$hButton = GUICtrlCreateButton("Press me!", 320, 260, 70, 25)

GUICtrlSetOnEvent($hLabel, "LabelFunction") ; Set the label control's function GUICtrlSetOnEvent($hButton, "ButtonFunction") ; The button's function

GUISetOnEvent($GUI_EVENT_CLOSE, "ExitGUI") ; What function to call when we try close the GUI ;Show the GUI. We need this line, or our GUI will NOT be displayed!

GUISetState(@SW_SHOW)

;Endless While loop to keep the GUI Open While 1

Sleep(10); So we don't use heaps of CPU WEnd

Func LabelFunction()

MsgBox(0, "Hello!", "We have clicked on the label!"); Say Hello EndFunc ;==>LabelFunction

Func ButtonFunction()

$read = GUICtrlRead($hInput)

;Check to see if we have /SPECIAL using StringInStr.

If StringInStr($read, "/SPECIAL") Then

;We have it, display the message.

MsgBox(0, "WOW!", "This is a special message!") Else

;Get Existing Data of edit

$read2 = GUICtrlRead($hEdit)

$text = $read2 & @CRLF & $read ; Join the existing and the new text seperated by a line.

GUICtrlSetData($hEdit, $text) ; Set the edit control to have our new data!

GUICtrlSetData($hInput, "");Reset the data of the input.

EndIf

EndFunc ;==>ButtonFunction Func ExitGui ()

Exit ; Exit the program EndFunc

LEARNING TO SCRIPT WITH AUTOIT V3 Page 48

Related documents