User:Trids
Just an avid AU3 fan ;o)
experimenting with layout etc.
Tutorial: Automating MSPaint to play Hangman
Lesson 1: Starting an application
- Starts MSPaint, displays a welcome message.
- Illustrates Run, RunWait, WinSetTitle, MsgBox, SplashTextOn, SplashOff
We're going to automate MSPaint to play Hangman. So lets start by launching mspaint.exe. According to the AU3 helpfile, we can use either Run or RunWait. The difference lies in when we get control back from the command.
Lets see this in action. Create a file called Hangman.au3 and copy in the following lines:
; ; Hangman with MSPaint ; ;we don't need to specify the path if the exe is in the system directory.. RunWait ("MSPAINT.EXE") MsgBox (0,"","Script continues")
When you run this, notice that the MsgBox pops up only after we shutdown mspaint. This won't do, because the MsgBox is standing in for the rest of the code that we still need to write. Lets see what happens if we use Run instead, so that the MsgBox comes up while mspaint is still running. Also, notice that the title of the mspaint window says "untitled - Paint": we'll change this to show that we're playing Hangman, and display a splash page for the same reason. We'll also maximise the mspaint window (see the change to the Run command).
We'll be using the title in some further commands, so lets create a variable for it instead of typing it out each time (and possibly misspelling it!):
; ; Hangman with MSPaint ; Dim $sTitle = "Hangman by AutoIt" ;Get things going SplashTextOn("Hangman", "This is a tutorial on how to use AutoIt v3",-1,-1,-1,-1,16) Run ("MSPAINT.EXE", "", @SW_MAXIMIZE) WinSetTitle ("untitled - Paint", "", $sTitle) Sleep (2000) SplashOff () MsgBox (0,"","Waiting")
But when we run this, we see that something is wrong: the title hasn't changed! And according to the helpfile, our syntax is correct. The problem is that mspaint takes a little longer to create the window that says "untitled - Paint" than the script takes to get to the point where it wants to change it. So lets just make sure with WinWait that the window exists before we change it with WinSetTitle.
At the same time, we'll change the dimensions of the splash page from the defaults, by altering the parameters to the SplashTextOn statement.
One last step for this lesson - if you've been running the scripts at each step as we've tested our changes (which is the best way to learn), you'll have several instances of mspaint hanging around unless you've closed them manually. So lets also add a feature that closes mspaint when the script terminates, using ProcessClose and capturing the Process ID for it from the Run command. Here's the final script for this stage:
; ; Hangman with MSPaint ; Dim $sTitle = "Hangman by AutoIt" ;Get things going SplashTextOn ("Hangman", "This is a tutorial on how to use AutoIt v3",400,100,-1,-1,16) $nPID = Run ("MSPAINT.EXE", "", @SW_MAXIMIZE) WinWait ("untitled - Paint") WinSetTitle ("untitled - Paint", "", $sTitle) Sleep (2000) SplashOff () MsgBox (0,"","Waiting") ProcessClose ($nPID)
- Sets Image size, saves/opens a file.
- Illustrates <Various way of navigating a menu>
Discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion
code code code
Discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion
code code code
Lesson 3: Clicks and Drags
- Draws some shapes in different colours, introduces AutoitSpy.
- Illustrates AutoitSetOption, Opt, MouseCoordMode, MouseMove, MouseClick, MouseClickDrag, MouseDown, MouseUp
Discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion
code code code
Discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion discussion
code code code
Lesson 4: Arrays and ini files
- Prepares for the game ahead, reads previous game settings from an INI file, loads arrays to be used in the game.
- Illustrates Dim, INIRead, INIWrite, InputBox
etc .. you know, that kind of thing .. each lesson building on the previous one, and showing good practice: indentation, naming standards, use of UDFs