Tutorial hangman: Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
(This is unfinished. I won't link to this on the tutorials page.)
 
m (BrokenLinks(FunctionList): ...)
Line 6: Line 6:
* Illustrates Run, RunWait, WinSetTitle, MsgBox, SplashTextOn, SplashOff
* 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.
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 {{Help File|Run}} or {{Help File|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:
Lets see this in action. Create a file called '''Hangman.au3''' and copy in the following lines:
      
      
<syntaxhighlight lang="autoit">
  ;
  ;
  ; Hangman with MSPaint
  ; Hangman with MSPaint
Line 17: Line 18:
      
      
     MsgBox (0,"","Script continues")
     MsgBox (0,"","Script continues")
</syntaxhighlight>


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).
When you run this, notice that the {{Help File|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!):
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!):


<syntaxhighlight lang="autoit">
  ;
  ;
  ; Hangman with MSPaint
  ; Hangman with MSPaint
Line 35: Line 38:
      
      
     MsgBox (0,"","Waiting")
     MsgBox (0,"","Waiting")
</syntaxhighlight>


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]].  
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 {{Help File|WinWait}} that the ''window exists'' before we change it with {{Help File|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.  
At the same time, we'll change the dimensions of the splash page from the defaults, by altering the parameters to the {{Help File|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:
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 {{Help File|ProcessClose}} and capturing the Process ID for it from the '''Run''' command. Here's the final script for this stage:


<syntaxhighlight lang="autoit">
  ;
  ;
  ; Hangman with MSPaint
  ; Hangman with MSPaint
Line 58: Line 63:
     MsgBox (0,"","Waiting")
     MsgBox (0,"","Waiting")
     ProcessClose ($nPID)
     ProcessClose ($nPID)
</syntaxhighlight>

Revision as of 13:47, 17 November 2012

This page is still a work in progress.

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)
     ;If you become stuck on the splash screen, capitalize the "u" in "untitled - Paint" on the following two lines to correct it
     WinWait ("untitled - Paint")
     WinSetTitle ("untitled - Paint", "", $sTitle)
     Sleep (2000)
     SplashOff ()
     
     MsgBox (0,"","Waiting")
     ProcessClose ($nPID)