Home
Research
Photography
Projects
Code Samples
Enhance your applications with Microsoft Agent
This article, written by Leo C. Singleton IV, originally appeared on-line on the Newtech Developers Journal in December, 1998.

Microsoft® Agent is a new and exciting technology, which allows developers to create and use animated characters that can interact with users in virtually any application. Computer users will no longer have to seek help through the typical, dull, gray dialog boxes, but can now get information through friendly, entertaining characters.

Agent offers many advantages to programmers. First, Agent supports the Microsoft Speech API, which allows both the character to speak with the user, and the user to speak back to the character. The programmer has total control over characters in Agent. The characters are free-floating, and can be placed or moved anywhere on the screen by the user or the programmer at any time. In addition, each of the Agent characters contain numerous animation sequences, giving the developer nearly unlimited flexibility.

Another benefit of Agent is its ease of use for programmers. With only minimal experience, any developer can easily produce enjoyable, animated, and even voice-activated applications and presentations. The programming required to use Agent is usually only a few lines long, and it is often shorter than the equivalent code used to display and update a dialog box.

Agent can be used with any Microsoft Windows® application that supports ActiveX® technology. This includes numerous programming and scripting languages. You can even use these languages through Microsoft Office macros, web pages viewed with Internet Explorer, and some e-mail readers.

The Cast of Agent

Four characters are available for Agent from the Microsoft web site, each one with dozens of animation sequences. And if you are artistically inclined, Microsoft has released a character editor that lets you use your own creations with Agent. The possibilities are unlimited. You could make your favorite animal or cartoon character, or even put picture of yourself on the screen.

Getting Started

   

Before you can use Agent, you must download the components from the Microsoft web site. For this example, you must download the Microsoft Agent core components (395k) and the Peedy character file (3.3mb). You may also want to download the other three character files (5.6mb) and the Lernout & Hauspie TruVoice Text-To-Speech Engine (1mb). The first example does not require these files, but you will need them later. Follow each component's instructions for installation.

 
Microsoft Agent Downloads

Table 1 - System Requirements
Pentium 100-MHz PC (or faster)
at least 16 MB of RAM
Microsoft Windows 95, 98, or NT 4.0 (x86)
Microsoft Visual Basic 5.0 or higher
Microsoft Agent core components
Peedy character file

Create a new Standard EXE project in Microsoft Visual Basic®. Add the Agent control to your new project by selecting Components from the Project menu, and checking the box next to Microsoft Agent Control 2.0. Insert the Agent control on Form1 of your application.

Add this line to the declarations section of code for Form1:
Dim Peedy as IAgentCtlCharacter

Add the following lines to the code for Form1:
Private Sub Form_Load()
  Agent1.Characters.Load "Peedy", _
    "peedy.acs"

  Set Peedy = _
    Agent1.Characters.Character("Peedy")
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Agent1.Characters.Unload "Peedy"
End Sub

If you try to start the application now, nothing will happen. Why? Although the first line of the Load event loaded Peedy into memory, your application never told the Agent control to show him. To solve this, insert two command buttons on Form1. Name the first one cmdShow and the second cmdHide. Change their captions to Show and Hide.

Then, add the following lines of code to Form1:
Private Sub cmdShow_Click()
  Peedy.Show
End Sub

Private Sub cmdHide_Click()
  Peedy.Hide
End Sub

Now start your application and click the Show button. Peedy should fly to the corner of your screen and sit there, until you press the Hide button or close the application.

You will notice that Peedy does not always sit still while he is on the screen. This is because Agent characters have idle animations, which occur when there are no actions in the message queue of the character. If you leave Peedy alone for a few minutes, you can watch his some of his idle animations.

Teaching Your Bird to Talk

Now that you can make Peedy show and hide in your application, the next step is to have him interact with the user. One easy way is through the Speak method, which displays a word balloon containing a message, similar to a comic strip. If you have a text-to-speech engine installed, your Agent character will even be able to speak.

To continue with this example, you will want to download the Lernout & Hauspie TruVoice Text-To-Speech Engine (1mb) from the Microsoft web site. This example will run without it, but the characters will not have audio.

Continue with the application from the previous section, and add a text box to Form1. Name this text box txtSpeech. Also, add another command button to the form, and name it cmdSpeak. Set the caption of the command button to Speak, and delete the text inside the text box.

Next, add the following event handler for the Click event of cmdSpeak:
Private Sub cmdSpeak_Click()
  Peedy.Speak txtSpeech
End Sub

Now start your application and click the Show button. Then enter something into the text box, and click the Speak button. Peedy will read the text in the txtSpeech box.

Move It, Bird!

The top-left corner of the screen is not always the ideal position for Peedy. This part of the screen often contains menus and toolbars, where Agent characters can get in the way. As you may have already noticed, the user can drag Agent characters on the screen, but characters also have a method called MoveTo, which allows developers to control the position of the characters.

MoveTo requires two parameters: the x and y screen coordinates of the new position. These are measured in pixels from the top-left corner of the character. If MoveTo is called before Show, the character will appear at the new position. Otherwise, the character will start at its original position, and move to the new position. In the case of Peedy, a flying animation will be shown.

Add two new command buttons to the example from the previous section, and name them cmdPosition1 and cmdPosition2. The Position1 button will move Peedy to the top-left corner of the screen, and the Position2 button will move him to the bottom-right corner.

Add these lines for the Click event of cmdPosition1:
Private Sub cmdPosition1_Click()
  'Move Peedy to the top-left corner of
  'the screen
  Peedy.MoveTo 0, 0
End Sub

The code for the first position was simple, but the second will be much more complicated. First, we will have to determine the resolution of the user's screen, by using the Screen.Width and Screen.Height properties. Since Screen.Width and Screen.Height return measurements in twips, we then must convert these values into pixels. Finally, we must account for the width and height of Peedy, since the MoveTo method uses the coordinates of the top-left of the character.

This is the code for cmdPosition2, which will move Peedy to the bottom-right corner of the screen:
Private Sub cmdPosition2_Click()
Dim nScreenWidth, nScreenHeight As Integer
Dim nPeedyWidth, nPeedyHeight As Integer

  'Get screen width and height (in pixels)
  nScreenWidth = Screen.Width / _
    Screen.TwipsPerPixelX
  nScreenHeight = Screen.Height / _
    Screen.TwipsPerPixelY

  'Get Peedy's width and height (in pixels)
  nPeedyWidth = Peedy.Width
  nPeedyHeight = Peedy.Height

  'Move Peedy to bottom-right corner of
  'screen
  Peedy.MoveTo nScreenWidth - _
    nPeedyWidth, nScreenHeight – _
    nPeedyHeight
End Sub

Now start your application, and show Peedy. When you click the Position2 button, he will fly to the bottom-right corner of your screen. Press Position1, and he will fly back to his original position. Also, notice that if you attempt to move Peedy while he is hidden, he will appear in the new position the next time he is shown.

The Play Method

There is one important method in Agent characters that we have not covered: the Play method, which plays one of the animation sequences of the character. It takes one parameter, a string containing the name of the animation to play. A complete list of animations for each character is available in the Microsoft Agent documentation from the Microsoft web site, but a shortened version is shown below in Table 2.

Table 2 - Peedy Animation Sequences
AnimationReturn AnimationSupports SpeakingDescription
Announceexit branchesyesPaper airplane flies in and unfolds
Congratulateexit branchesyesDisplays blue ribbons
DoMagic1noneyesRaises magic wand
GetAttentionGetAttentionReturnyesJumps up with wings outstretched
Greetexit branchesyesBows
Pleasedexit branchesyesSmiles
ProcessnonenoUses calculator
ReadReadReturnyesOpens magazine, reads, and looks up
RestPosenoneyesNeutral position
SearchnonenoRotates with telescope
Suggestexit branchesyesDisplays light bulb
Thinkexit branchesyesLooks up with wing on face
Waveexit branchesyesWaves
WriteWriteReturnyesTakes out pencil and pad, writes, and looks up

Add a command button named cmdProcess to the form of the example from the previous section. Change the caption of the button to Process. For the Click event of cmdProcess, add the following lines:
Private Sub cmdProcess_Click()
  Peedy.Play "Process"
End Sub

Start your application, show Peedy, then press the Process button. Peedy will use a calculator, and then return to his normal position.

Create another button, named cmdWave, the same as the step above, except change the code for the Click event to:
Private Sub cmdWave_Click()
  Peedy.Play "Wave"
End Sub

When you start your application and press the Wave button, Peedy will wave, but he will remain frozen in the last frame of the animation sequence for a few seconds. How can you prevent this?

The problem is simple. Certain animations do not return the character to their neutral position until another animation is played. They use return animations called exit branches. Since the animations are queued, Peedy has reached the end of his queue, so he freezes at the last frame. Once no new animations enter the queue for a few seconds, he automatically enters his idle sequences, and returns to his normal position.

To prevent this, change the code for the Click event of cmdWave to the following:
Private Sub cmdWave_Click()
  Peedy.Play "Wave"
  Peedy.Play "RestPose"
End Sub

Now when you start the application and click the Wave button, Peedy will wave, and then return to his normal position.

Some animations, such as GetAttention and Read, require special return animations. Simply call the Play method with the name of the return animation immediately after calling the original animation.

Another feature of the Play method is the ability for a character to speak during an animation sequence. Not all animations support this feature (see table 2).

Place one more push button on the application, and name it cmdGreet. Add the following code to handle the Click event of cmdGreet:
Private Sub cmdGreet_Click()
  Peedy.Play "Greet"
  Peedy.Speak "Hello, I am Peedy."
  Peedy.Play "RestPose"
End Sub

Start the application, show Peedy, and press the Greet button. Peedy will bow and say hello.

Complete Source Code Listing

Dim Peedy As IAgentCtlCharacter

Private Sub cmdGreet_Click()
  Peedy.Play "Greet"
  Peedy.Speak "Hello, I am Peedy."
  Peedy.Play "RestPose"
End Sub

Private Sub cmdHide_Click()
  Peedy.Hide
End Sub

Private Sub cmdPosition1_Click()
  'Move Peedy to the top-left corner of
  'the screen
  Peedy.MoveTo 0, 0
End Sub

Private Sub cmdPosition2_Click()
Dim nScreenWidth, nScreenHeight As Integer
Dim nPeedyWidth, nPeedyHeight As Integer
  'Get screen width and height (in pixels)
  nScreenWidth = Screen.Width / _
    Screen.TwipsPerPixelX
  nScreenHeight = Screen.Height / _
    Screen.TwipsPerPixelY

  'Get Peedy's width and height (in pixels)
  nPeedyWidth = Peedy.Width
  nPeedyHeight = Peedy.Height

  'Move Peedy to bottom-right corner of
  'screen
  Peedy.MoveTo nScreenWidth - _
    nPeedyWidth, nScreenHeight - _
    nPeedyHeight
End Sub

Private Sub cmdProcess_Click()
  Peedy.Play "Process"
End Sub

Private Sub cmdShow_Click()
  Peedy.Show
End Sub

Private Sub cmdSpeak_Click()
  Peedy.Speak txtSpeech
End Sub

Private Sub cmdWave_Click()
  Peedy.Play "Wave"
  Peedy.Play "RestPose"
End Sub

Private Sub Form_Load()
  Agent1.Characters.Load "Peedy", _
    "peedy.acs"

  Set Peedy = _
    Agent1.Characters.Character("Peedy")
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Agent1.Characters.Unload "Peedy"
End Sub

   
Conclusion

Microsoft Agent is a quick and easy way to enhance any application. The example code in this article was only a few lines long, but demonstrated numerous features of Agent, including speech, animations, and character movement.

There are still many advanced features of Agent that were not covered in this article. Agent comes with three other characters, supports multiple characters on the screen at once, and supports voice-recognition. The Microsoft Agent documentation covers many of these topics.

 
Microsoft Agent Web Site


This article, written by Leo C. Singleton IV, originally appeared on-line on the Newtech Developers Journal in December, 1998.


Microsoft, Windows, ActiveX, and Visual Basic are registered trademarks of Microsoft Corporation in the United States and/or other countries. Screen shots containing Peedy used by permission of Microsoft Corporation. © 1993-1998 Microsoft Corporation.  All rights reserved.

© 1998-2007 Leo C. Singleton IV