Part IV: Using ActiveX Controls

In this part of the project, you will insert an ActiveX control, the Microsoft Agent, into the DEFAULT.HTM file. The Agent control presents an animated character that can help users navigate your site. In this exercise, the Agent control will provide help in locating and ordering a book.

Step 1

Before you can use the Agent control, you must install it on your machine. Run the setup program provided on the CD-ROM that accompanies this book. Place the control in a directory named C:\PROGRAM FILES\MICROSOFT AGENT. Once installed, the Agent is just like any other ActiveX control you might use.

Step 2

If you don't already have the Bookstore project open, open it now. Open DEFAULT.HTM in the Visual InterDev text editor. Locate the beginning of the Web page body, as indicated by the <BODY> tag. Create a new line below the <BODY> tag, and place your cursor there. This is where you will insert the code to render the Agent.

Insert the Agent control by selecting Into HTML/ActiveX Control from the Insert menu. In the ActiveX Control dialog box, locate the Microsoft Agent control, select the control, and click the OK button. Visual InterDev will display an icon representing the Agent control and a Properties dialog box.

Click the All tab, and examine all the properties of the Agent control. Change its ID property to Wizard. If you were to actually use this control in a Web site, you would also have to set the CodeBase property of the Agent to refer to the location where the files for the ActiveX control were kept. This allows a client browser to download the Agent control automatically if it is not installed on the client's machine. Internet Explorer 4.0 ships with the Agent control, so no CODEBASE attribute is required.

Return to the code by closing the Properties window and the Graphical Layout window. You should see the following code added by Visual InterDev:

<OBJECT ID="Wizard" WIDTH=100 HEIGHT=51     CLASSID="CLSID:F5BE8BD2-7DE6-11D0-91FE-00C04FD701A5"> </OBJECT>

The <OBJECT> tag is used to insert an ActiveX control in the Web page. Internet Explorer determines which control to insert based on the CLSID attribute, which is a unique serial number for the requested control. The <OBJECT> tag is sufficient to address the ActiveX control, but the Agent control has no visible interface until you load one of the character files that contain the animations for the Agent.

The Agent character should be loaded when the page is first loaded into the browser. This can be accomplished with the Window_OnLoad event. In the same SCRIPT section where you created the monthly special, add the following code:

Sub Window_OnLoad() End Sub

You will load the Agent animation files in the OnLoad event. These files exist as ACS files, which are typically loaded during the setup process for the Microsoft Agent. This exercise assumes that you have installed the Agent in the directory C:\PROGRAM FILES\MICROSOFT AGENT and also assumes that the character files are located in C:\PROGRAM FILES\MICROSOFT AGENT\CHARACTERS. If you have installed the files in a different location, you must modify the code accordingly.

Add the following code to the OnLoad event to load the Agent character:

` Initialize the variables when the page ` is fully loaded ` Dimension variables Dim Agents Dim AgentPath ` Set variables Set Agents = Wizard.Characters AgentPath = "c:\program files\microsoft agent\characters\" ` Load character Agents.Load "WIZARD", AgentPath & "Merlin.acs"

Save your work, and preview DEFAULT.HTM by selecting Preview In Browser from the File menu. The Agent should be visible.

Step 3

Now you can add some functionality to the Agent. The Agent control supports custom commands, which you can assign to the Agent as soon as the entire Web page is loaded. The custom commands are displayed whenever the user right-clicks the Agent. Use the OnLoad event of the Window object to create the custom commands. Here is the code:

` Create custom commands Agents("WIZARD").Commands.Caption = "Custom Commands" ` Name, Caption, Voice Command, Enabled, Visible Agents("WIZARD").Commands.Add "Search", "Search", "Search", True, True Agents("WIZARD").Commands.Add "Find Book", "Find Book", "Find Book", _                               True, True ` Show Agent Agents("WIZARD").Show Agents("WIZARD").Play "Surprised" Agents("WIZARD").Speak "Welcome to WebPages!"

Place the following code in the SCRIPT section to hide the Agent when the user exits the current page:

Sub Window_OnUnload()     Wizard.Characters("WIZARD").Hide End Sub

Step 4

When a user right-clicks the Agent and chooses a custom command, the Agent control fires the Command event and the Command event performs the function appropriate to the menu item. In this step of the exercise, you will direct the browser to the SEARCH.HTM page whenever the Search item is selected from the Agent menu. Add the following code to a new SCRIPT section to handle the Command event:

<SCRIPT LANGUAGE="VBScript"> <!-- Sub Wizard_Command(UserInput)     ` Set variables     Dim Agent     Set Agent = Wizard.Characters("WIZARD")     ` Go to search page     If UserInput.Voice = "Search" Or UserInput.Name = "Search" Then         Window.Navigate "search.htm"            

    End If End Sub --> </SCRIPT>

When your program navigates to the search page, the Agent will reappear through the use of an <OBJECT> tag. Although the Agent is actually hiding between pages, it seems to be present throughout the site.

Step 5

Save your work, and open DEFAULT.HTM in Internet Explorer. When the page opens and the Agent becomes visible (Figure 6-6), right-click the Agent and choose Search from the pop-up menu. The Agent navigates to the search page and gives you some instructions. If you have a multimedia computer with a working microphone, try speaking a command to the Agent!

Figure 6-6. The Agent control.

Step 6

Open SEARCH.HTM in Visual InterDev. You need to add functionality to the Agent, enabling it to respond when a user navigates to the search page either by using the hyperlink on the home page or by commanding the Agent.

Insert the Agent control into SEARCH.HTM just as you did for the home page. Add a SCRIPT section to your code to load the character animations and to take action when the page is loaded. Use the following code:

<SCRIPT LANGUAGE="VBScript"> <!--     Sub Window_OnLoad()         ` Dimension variables         Dim Agents         Dim AgentPath                  ` Set variables         Set Agents = Wizard.Characters         AgentPath = "c:\program files\microsoft agent\characters\"         ` Load character         Agents.Load "WIZARD", AgentPath & "Merlin.acs"         ` Show Agent         Agents("WIZARD").Show         ` Move Agent         Agents("WIZARD").MoveTo 0, 0         ` Give instructions         Agents("WIZARD").Speak _             "Use this page to perform full text searches."         ` Move again         Agents("WIZARD").MoveTo Window.Screen.Width - 100, 0     End Sub     Sub Window_OnUnload()         Wizard.Characters("WIZARD").Hide     End Sub --> </SCRIPT>



Programming Active Server Pages
Programming Active Server Pages (Microsoft Programming Series)
ISBN: 1572317000
EAN: 2147483647
Year: 1996
Pages: 84

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net