Tutorial Thursday - Hello World in a GUI
Thursday, October 11th, 2007Tutorial Thursday
Note to self: make crappy logo.
So, last week, we made a messagebox.
That was totally fun, wasn’t it?
You bet your ass it was.
But now, we’re going to do the same thing, except this time, we’re also making a Graphics User Interface (GUI) around it, which is just a tiny bit trickier.
First things first; start your favorite code editor (PSPad *hint hint*) and type the following:
#include <guiconstants.au3></guiconstants.au3>
Obviously, you will need the AutoIt package.
Now, what this basically does is add some functionality through a set of functions registered in GUIConstants.au3, including a very special one I’m about to introduce.
So, on to the window making.
GUICreate("Hello World!",256,256)
GUISetState(@SW_SHOW)
This makes a window with a caption of “Hello World!” with a width of 256 pixels, a height of 256 pixels and it’s displayed in the center of the screen.
I know you’re now thinking: “YESSSSS. A CODER IS ME.”, but nope, sorry.
This doesn’t really do anything besides open a window and then close it really really fast.
No, what we need is some way to keep it open…
Hmm…
How about…
We feed the program a useless loop that will keep it open… forever!
AWESOOOME.
While 1+1=2
If GUIGetMSG() = Then ExitLoop
Wend
This so called while-loop does exactly what you think it does.
Basically it translates into “as long as condition is met do following action”
For example:
As long as Timmy is being a dick, keep punching him.
In actual code, this would translate into:
While == “dick” funPunch() Wend
I hope that makes sort of sense.
The second thing I need to explain is the If… Then…
In proper English, the syntax would be:
Timmy is gay, so I punch it out of him.
In code:
If == “gay” Then funPunch()
Poor Timmy, nobody seems to like him.
This line:
If GUIGetMSG() = Then ExitLoop
Means that when you click the little x in the corner, the program closes down, because the infinite While loop is exited.
Which is weird, I guess, seeing as it is INFINITE.
To summarize, we now have:
-an empty window
#include <GUIConstants.au3>
GUICreate("Hello World!",256,256)
GUISetState(@SW_SHOW)
While 1+1=2
If GUIGetMSG() = Then ExitLoop
Wend
AWESOME.
Time to add stuff.
We update the code as follows, replacing the old crap:
#include <GUIConstants.au3>
GUICreate("Hello World!",256,256)
GUICreateLabel(“Hello World!”, 112,32, 256,24)
GUISetState(@SW_SHOW)
While 1+1=2
If GUIGetMSG() = Then ExitLoop
Wend
The GUICreateLabel bit adds… a text… thingy.
It’s a label I guess.
It’s built up quite easily:
-”the text you want to display between brackets”
-start x coordinate
-start y coordinate
-width of control
-height of control
Where (0,0) is the top-left corner of the screen.
Personally I think AutoIt’s display system is not that awesome.
I would’ve preferred if it was (startx,starty, endx,endy), but you can’t have it all I guess.
You have to… row… with the… belts… you have.
The AutoIt manual has many more types of controls, at the section Function Reference -> GUI Reference -> GUI Control Creation.
So try some funky combinations, because next week, we’re going to do Functions and Variables.
AND DO YOUR HOMEWORK BECAUSE THE NEXT ASSIGNMENT ACCOUNTS FOR TWENTY PERCENT OF YOUR GRADE.
Class dismissed.
Previous tutorials:
#01: Hello World in AutoIt
-knite
