1. Trang chủ
  2. » Công Nghệ Thông Tin

Microsoft WSH and VBScript Programming for the Absolute Beginner Part 44 pptx

10 187 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 124,8 KB

Nội dung

410 //Main Processing Section //Verify that the user wants to open the VBScript Game Console intReply = DisplayInitialSplashScreen(); //intReply will be set equal to 2 if the user clicks on the Cancel if (intReply == 2) { //Close the VBScript Game Console WScript.Quit(); } //Procedure Section //This procedure prompts the user for confirmation function DisplayInitialSplashScreen() { strTitleBarMsg = getResource(“cTitlebarMsg”); //Display popup dialog using the WshShell object’s Popup() method intResults = + objWshShl.Popup(strWelcome + strInstructions, 0, strTitleBarMsg, 1); //Return the result to the calling statement return intResults } One way to develop each of the three scripts used in this Windows Script File is to create each script as a stand-alone script and get them all working as expected, and then to cut and paste the scripts into the Windows Script File in the areas identified for each script by the XML tags. As you can see, this JScript is broken down into the same three sections that I’ve been using to organize this book’s VBScripts (that is, the initialization section, the main processing sec- tion, and the procedure section). Comments in JScript are created using the // characters, and I have added a number of them to the script to explain its operation. The script’s only TRICK Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition function, DisplayInitialSplashScreen(), is responsible for displaying the VBScript Game Console’s initial splash screen, which it does using the WshShell object’s Popup() method. JScript does not provide any functions that work similarly to the VBScript MsgBox() or InputBox() functions. Therefore, to display text in a pop-up dialog using JScript, you must use either the WshShell object’s Popup() method or the WScript object’s Echo() method. Developing the VBScript Game Console The VBScript portion of the VBScript Game Console contains the bulk of the complexity and programming logic. The first step in developing this VBScript is to insert your VBScript tem- plate and fill it in, as follows: ‘******************************************************************* ‘Script Name: N/A ‘Author: Jerry Ford ‘Created: 12/20/02 ‘Description: This VBScript displays the actual VBScript ‘ Game Console interface and interacts with the user ‘******************************************************************* ‘Initialization Section Option Explicit Defining the Elements in the Initialization Section Next, let’s define the variables, objects, and the array used by the VBScript. In most of the VBScripts that you’ve seen in this book, I’ve included a constant that defines the titlebar message to be displayed in the script’s pop-up dialogs. However, this time I’ve omitted this constant in the VBScript because I have, instead, defined this value using the <reference> and </reference> tags at the beginning of the Windows Script File. This allows me to retrieve the constant and create a standard titlebar message for every script defined in the Windows Script File. Dim objFsoObject, objWshShl, strPlayOrNot, strConsoleStatus Dim objGameFolder, objGames, strSelection, objWordList Dim strFileString, intCount, strDisplayString, intNoFilesFound Dim strTitleBarMsg, intResults Dim ConsoleArray() 411 Chapter 12 • Combining Different Scripting Languages 412 ‘Set up an instance of the FileSystemObject Set objFsoObject = CreateObject(“Scripting.FileSystemObject”) ‘Set up an instance of the WshShell Set objWshShl = WScript.CreateObject(“WScript.Shell”) ‘Retrieve the titlebar message to the displayed in popup dialogs strTitleBarMsg = getResource(“cTitlebarMsg”) Building the Main Processing Section The statements listed in the Main Processing section are straightforward. I began by first checking the value of intResults, which was set by the previous JScript. If intResults is equal to 2, then the player told the JScript to shut down the game console. However, after executing the WScript object’s Quit() method, inside the JScript, the WSF script keeps run- ning, executing the VBScript. Therefore, you’ll need to include this additional check and execute the WScript object’s Quit() method a second time to prevent the VBScript from dis- playing the game console. I then used the FileSystemObject object’s GetFolderMethod() to establish a reference to the location where the VBScript games to be displayed in the game console are stored. A For Each loop that spins through the list of files stored in this folder, keeping a record of the number of files counted, is executed. Note as the VBScript is currently written, it expects to find only VBScript files stored in the game folder. Therefore, no steps have been taken to filter out other file types. If you plan to store different files in the game folder, you will need to add additional logic to the VBScript to prevent it from displaying those files as well. Next, the VBScript’s array is resized according to the number of files found. This array is used to store the names of each VBScript game and to associate each VBScript game with its assigned number as shown in the game console’s dialog. Finally, the ConsoleLoop() function is called. This function is responsible for the overall operation of the VBScript game console. ‘Main Processing Section If intResults = 2 Then WScript.Quit() End If TRAP Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition ‘Specify the location of the folder where word files are stored Set objGameFolder = objFsoObject.GetFolder(“C:\VBScriptGames”) ‘Get a list of files stored in the folder Set objGames = objGameFolder.Files ‘Look and count the number of words files For Each objWordList In objGames intNoFilesFound = intNoFilesFound + 1 Next ‘Redefine the script’s array based on number of word files found ReDim ConsoleArray(intNoFilesFound) ‘Call the function that displays the VBScript Game Console ConsoleLoop() Creating the ConsoleLoop() Function The VBScript Game Console is controlled by the ConsoleLoop() function. This function is responsible for assigning a number to each VBScript, for loading the VBScript’s array, for interrogating user input, and for performing the appropriate action based on that input. ‘This function displays the VBScript Game Console, accepts user ‘input, validates the input and starts other VBScript games Function ConsoleLoop() ‘This string contains a list of all the word files discovered ‘in the target folder strSelection = “” ‘This counter will be used to track individual word files and ‘will be kept in sink with array entries intCount = 0 ‘Loop through the list of word files For Each objWordList In objGames ‘Build a master string containing a list of all the word files 413 Chapter 12 • Combining Different Scripting Languages 414 ‘But exclude the VBScriptGameConsole.wsf file from this list If objWordList.Name <> “VBScriptGameConsole.wsf” Then ‘Increment count each time through the loop intCount = intCount + 1 strFileString = strFileString & “ “ & objWordList.Name ‘Build another list, adding number for later display strSelection = strSelection & intCount & “. “ & _ objWordList.Name & vbCrLf ‘Load the name of each script into the array ConsoleArray(intCount) = objWordList.Name End If Next ‘This variable is used to determine when to close the console strConsoleStatus = “Active” ‘Create loop & keep it running until the user decides to close it Do Until strConsoleStatus = “Terminate” ‘Interrogate the user’s input strPlayOrNot = UCase(PickAGame()) ‘If the user did not type anything or if he or she clicked on ‘Cancel then exit the function let things come to an end If strPlayOrNot = “” Then Exit Function End If ‘Define a Select Case statement and use it to test the various ‘possible types of user input Select Case UCase(strPlayOrNot) Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition ‘If the user typed QUIT then exit the function let things ‘come to an end Case “QUIT” Exit Function ‘If the user typed ABOUT call the function that displays ‘additional information abut the VBScript Game Console Case “ABOUT” AboutFunction() ‘If the user typed HELP call the function that provides ‘additional help information Case “HELP” HelpFunction() ‘Otherwise call the function that runs the selected VBScript Case Else ValidateAndRun()’ End Select Loop End Function Creating the ValidateAndRun() Function When called by the ConsoleLoop() function, the ValidateAndRun() function, shown below, validates user input by making sure that the user has supplied either a valid game number or valid game name. If a valid number or name is not supplied, then the function calls the InvalidChoice() function, which displays a generic error message telling the user how to properly operate the VBScript Game Console. If a valid number or name is supplied, then the function calls the RunScript() function, which then executes the specified VBScript game. ‘This function validates user input and if appropriate calls ‘functions that display further instructions or run the selected ‘VBScript Function ValidateAndRun() 415 Chapter 12 • Combining Different Scripting Languages 416 ‘Check to see if the user provided a valid game number If IsNumeric(strPlayOrNot) <> 0 Then ‘Make sure that the user did not type a negative number If strPlayOrNot > 0 Then ‘Make sure that the user did not type a invalid number If CInt(strPlayOrNot) < CInt(intCount) Then ‘If the number is valid then find the associated script strPlayOrNot = ConsoleArray(strPlayOrNot) ‘Call the procedure that will then run the selected script RunScript() Else ‘Call this procedure if the user hast not typed a valid ‘script number InvalidChoice() End If Else InvalidChoice() End If ‘Check to see instead if the user provided a valid game name Else ‘Proceed only if the input typed by the user is a valid VBScript ‘game (e.g. its name appears in the previously built list of ‘VBScript game names If InStr(1, strSelection, strPlayOrNot, 1) > 1 Then ‘If the user didn’t type the .vbs file extension add it If InStr(1, strPlayOrNot, “.VBS”, 1) = 0 Then strPlayOrNot = strPlayOrNot & “.vbs” ‘Recheck to make sure that the script name is still valid If InStr(1, strSelection, strPlayOrNot, 1) > 1 Then ‘Call the procedure that runs the selected script RunScript() Else ‘Call this procedure if the user has not typed a valid ‘script name InvalidChoice() End If Else Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition ‘If the user specified the script’s .vbs file extension and ‘it is found in the previously built list of VBScript game ‘names then go ahead and call the procedure that will run ‘the script If InStr(1, strSelection, strPlayOrNot, 1) > 1 Then RunScript() Else ‘Run this procedure if user fails to supply valid input InvalidChoice() End If End If Else ‘If user supplied input is not found in the previously ‘built list of VBScript game names call this procedure InvalidChoice() End If End If End Function Creating the PickAGame() Function The PickAGame() function, shown next, is charged with displaying the contents of the VBScript game console whenever it is called. It does this by first building a primary display string that consists of a list of all VBScript games that have been found, as well as instruc- tions for getting Help, information about the script and its author, and for closing the game console. The display string, which is aptly named DisplayString, is then plugged into a VBScript InputBox() function, thus displaying information about your VBScript games and providing the user with a means of selecting those games. ‘This function displays the main VBScript game Console and collects ‘user input Function PickAGame() strDisplayString = strSelection & vbCrLf & _ “Or Type: [Help] [About] [Quit]” & vbCrLf PickAGame = InputBox(“W e l c o m e t o t h e” & vbCrLf & _ 417 Chapter 12 • Combining Different Scripting Languages 418 vbCrLf & “V B S c r i p t G a m e C o n s o l e !” & _ vbCrLf & vbCrLf & “Pick a Game:” & vbCrLf & vbCrLf & _ strDisplayString, strTitleBarMsg, “”, 50, 50) End Function By default, all WSH and VBScript pop-up dialogs are displayed in the middle of the display area. However, in the previous example I specified values of 50 and 50 as the last two attributes of the InputBox() function. These two values specify the location where the pop-up dialog should be displayed on the user’s screen. In this case, the pop-up dialog will be displayed in the upper-left corner of the screen. This keeps it handy without crowding the display area in the middle of the screen, where the VBScript games are displayed. Creating the RunScript() Function The RunScript() function, shown here, is very straightforward. When called, it uses the WshShell object’s Run() method to execute the VBScript selected by the user, as specified in the variable called PlayOrNot. ‘This function starts the VBScript selected by the user Function RunScript() objWshShl.Run “WScript “ & strPlayOrNot End Function Creating the InvalidChoice() Function The InvalidChoice() function, shown next, is responsible for displaying a generic error mes- sage using the VBScript MsgBox() function whenever the user provides the VBScript Game Console with invalid input. Examples of invalid input include numbers that have not been assigned to a VBScript listed in the console, such as –4 or 9999, as well as misspelled names of listed VBScript games. ‘This function is called whenever the user provides invalid input Function InvalidChoice() MsgBox “Sorry. Your selection was not valid. A valid “ & _ “selection consists of one of the following:” & vbCrLf & _ vbCrLf & “* The number associated with one of the listed “ & _ “VBScript games” & vbCrLf & “* The name of a listed “ & _ Microsoft WSH and VBScript Programming for the Absolute Beginner, Second Edition “VBScript game” & vbCrLf & “* The name of a listed “ & _ “VBScript game plus its file extension” & vbCrLf & _ “* Help - To view help information.” & vbCrLf & _ “* About - To view additional information about this game “ & _ “and its Author” & vbCrLf & “* Quit - To close the “ & _ “VBScript Game Console” & vbCrLf & vbCrLf & _ “Please try again.”, , strTitleBarMsg End Function Creating the HelpFunction() Function The HelpFunction() function, shown next, uses the VBScript MsgBox() function to display additional help information about the VBScript Game Console. It is called anytime the user types help and clicks OK. ‘This function displays help information in a popup dialog Function HelpFunction() MsgBox “Additional help information for the VBScript Game “ & _ “Console can be found at:” & vbCrLf & vbCrLf & _ “www.xxxxxxxx.com.”, , strTitleBarMsg End Function Creating the AboutFunction() Function The final function in the VBScript, shown next, is responsible for displaying information about the VBScript Game Console and its author. It is called whenever the user types about in the VBScript Game Console and clicks OK. As you can see, the function consists of a sin- gle statement that uses the MsgBox() function. The information included here is really just a brief template; I leave it to you to finish adding whatever content you think is appropriate. ‘This function displays information about the VBScript Game Console ‘and its author Function AboutFunction() MsgBox “VBScript Game Console © Jerry Ford 2002” & vbCrLf & _ vbCrLf & “Email the author at: xxxxx@xxxxxxxx.com.”, , strTitleBarMsg End Function 419 Chapter 12 • Combining Different Scripting Languages . created using the // characters, and I have added a number of them to the script to explain its operation. The script’s only TRICK Microsoft WSH and VBScript Programming for the Absolute Beginner, . method. Developing the VBScript Game Console The VBScript portion of the VBScript Game Console contains the bulk of the complexity and programming logic. The first step in developing this VBScript is. overall operation of the VBScript game console. ‘Main Processing Section If intResults = 2 Then WScript.Quit() End If TRAP Microsoft WSH and VBScript Programming for the Absolute Beginner, Second

Ngày đăng: 03/07/2014, 18:20

TỪ KHÓA LIÊN QUAN