Insider Power Techniques for Microsoft Windows XP [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Insider Power Techniques for Microsoft Windows XP [Electronic resources] - نسخه متنی

Paul McFedries

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
توضیحات
افزودن یادداشت جدید




Displaying More Detailed Information to the User

You saw earlier that the WScript object’s Echo method is useful for displaying simple text messages to the user. You can gain more control over the displayed message by using the WshShell object’s Popup method. WshShell refers to the Shell object that is exposed via the Automation interface of WScript. So before you can run the Popup method, you need to use CreateObject to create an instance of the WshShell object:

Set objWshShell = WScript.CreateObject(“WScript.Shell”)

If you’re familiar with Microsoft Visual Basic, the Popup method is similar to the MsgBox function in that it enables you to control both the dialog box title and the buttons that are displayed, as well as to determine which of those buttons the user pressed. Here’s the syntax:

Popup strText, [nSecondsToWait], [strTitle], [nType]


















strText


The message you want to display in the dialog box. (You can enter a string up to 1,024 characters long.)



nSecondsToWait


The maximum number of seconds the dialog box will be displayed.



strTitle


The text that appears in the dialog box title bar. If you omit this value, the string “Windows Script Host” appears in the title bar.



nType


A number or constant that specifies, among other things, the command buttons that appear in the dialog box (see “Setting the Style of the Message,” next). The default value is 0.


For example, the following statements display the dialog box shown in Figure 3-1:

Set objWshShell = WScript.CreateObject(“WScript.Shell”)
objWshShell.Popup “Couldn’t find MEMO.DOC!", , “Warning”


Figure 3-1: This is a simple message dialog box produced by the Popup method.





Tip

For long messages, VBScript wraps the text inside the dialog box. If you prefer to create your own line breaks, use VBScript’s vbCrLf constant to add a carriage-return and line-feed after a line:

objWshShell.Popup "First line" & vbCrLf & "Second line"



Setting the Style of the Message


The default Popup dialog box displays only an OK button. You can include other buttons and icons in the dialog box by using different values for the nType parameter. Table 3-1 lists the available options.




























































Table 3-1: The Popup method’s nType parameter options_ (continued)

VBScript Constant


Value


Description


Buttons


vbOKOnly


0


Displays only an OK button. (This is the default.)


vbOKCancel


1


Displays the OK and Cancel buttons.


vbAbortRetryIgnore


2


Displays the Abort, Retry, and Ignore buttons.


vbYesNoCancel


3


Displays the Yes, No, and Cancel buttons.


vbYesNo


4


Displays the Yes and No buttons.


vbRetryCancel


5


Displays the Retry and Cancel buttons.


Icons


vbCritical


16


Displays the Critical Message icon.


vbQuestion


32


Displays the Warning Query icon.


vbExclamation


48


Displays the Warning Message icon.


vbInformation


64


Displays the Information Message icon.


Default Button


vbDefaultButton1


0


The first button displayed is the default (that is, the button selected when the user presses Enter).


vbDefaultButton2


256


The second button displayed is the default.


vbDefaultButton3


512


The third button displayed is the default.



You derive the nType argument in one of two ways:



By adding up the values for each option.



By using the VBScript constants separated by plus signs (+).



The script below shows an example:

‘ First, set up the message  strText = “Are you sure you want to copy” & vbCrLf & _
“the selected files to drive A?" strTitle = “Copy Files" nType = vbYesNoCancel + vbQuestion + vbDefaultButton2 ‘ Now display it Set objWshShell = WScript.CreateObject(“WScript.Shell”)
intResult = objWshShell.Popup(strText, ,strTitle, nType)

Figure 3-2 shows the resulting dialog box. Here, three variables—strText, strTitle, and nType—store the values for the Popup method’s arguments. In particular, the following statement derives the nType argument:


Figure 3-2: This is the dialog box that’s displayed when you run the script.


nType = vbYesNoCancel + vbQuestion + vbDefaultButton2

You also could derive the nType argument by adding up the values that these constants represent (3, 32, and 256, respectively), but the script becomes less readable that way.


Getting Return Values from the Message Dialog Box


A dialog box that displays only an OK button is straightforward. The user either clicks OK or presses Enter to remove the dialog from the screen. The multibutton styles are a little different, however; the user has a choice of buttons to select, and your script needs a way to find out which button the user chose.

You do this by storing the Popup method’s return value in a variable. Table 3-2 lists the seven possible return values.

































Table 3-2: The Popup method’s return values

VBScript Constant


Value


Button Selected


vbOK


1


OK


vbCancel


2


Cancel


vbAbort


3


Abort


vbRetry


4


Retry


vbIgnore


5


Ignore


vbYes


6


Yes


vbNo


7


No


To process the return value, you can use an If...Then...Else or Select Case structure to test for the appropriate values. For example, the script shown earlier used a variable called intResult to store the return value of the Popup method. The following script shows a revised version of the script that uses a VBScript Select Case statement to test for the three possible return values, based on using vbYesNoCancel.

 ‘ First, set up the message
strText = “Are you sure you want to copy” & vbCrLf & _
“the selected files to drive A?"
strTitle = “Copy Files"
nType = vbYesNoCancel + vbQuestion + vbDefaultButton2
‘ Now display it
Set objWshShell = WScript.CreateObject(“WScript.Shell”)
intResult = objWshShell.Popup(strText, ,strTitle, nType)
‘ Process the result
Select Case intResult
Case vbYes
WScript.Echo “You clicked “"Yes"“!"
Case vbNo
WScript.Echo “You clicked “"No"“!"
Case vbCancel
WScript.Echo “You clicked “"Cancel"“!"
End Select

/ 126