Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#
Article

How to use Microsoft Agent in C#

Rate me:
Please Sign up or sign in to vote.
3.21/5 (20 votes)
7 Sep 20042 min read 138.8K   4.3K   45   31
An article explaining how to use Microsoft agents in a C# application

Introduction

Microsoft Agent is a technology used to add interactive animated characters to windows application or web page; these characters act according to the user input via speech recognition engine, and also they can speak and tell the user something via text-to-speech engine. Microsoft provides programmers four characters

  • A-Peddy
  • B-Genie
  • C-Merlin
  • D-Robby.

Each character has its own set of animation

Background

I’ve searched well so many sites for code that I can use MS agent to provide help to end user. After searching the C# books , I’ve found some nice code that helped me to create this simple Application . Hope it can help as a basic architecture.

Using the code

At first you should simply open VS.NET and then at the File menu click on New, Project. From the New Project Dialog Box, choose the Windows Application template project and name it WindowsApplication1 like shown below:

Image 1

After you create the windows add a button to it and name it speak and add a RichTextBox and name it talk , you should add to it Microsoft Agent component , to do that click on the Customize toolbox from tool menu then the following Dialog will appear

Image 2

Image 3

Select Microsoft Agent control 2.0 and click ok. Now in your tool box at the end of it you will find a new item added to it “Microsoft Agent”. Drag this item and drop it in your application, after drop “Microsoft Agent” the .Net will generate a new object from type AxAgentObjects.AxAgent as the following

C#
namespace WindowsApplication1
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
           private AxAgentObjects.AxAgent axAgent1;
           /// <summary>
           /// Required designer variable.
           /// </summary>
           private System.ComponentModel.Container components = null;
 
           public Form1()
           {
                   //
                   // Required for Windows Form Designer support
                   //
                   InitializeComponent();
 
                   //
                   // TODO: Add any constructor code 
                   // after InitializeComponent call
                   //
           }

Now create object of type AgentObjects.IAgentCtlCharacter and name this object speaker ,now at the load function write the following

C#
private void Form1_Load(object sender, System.EventArgs e)
           {
                   try
                   {                          
 
       //load the character  in the axAgent1 object -- 
      //axAgent1 can load more than one character
                    this.axAgent1.Characters.Load("Robby" , "robby.acs");   
 
      //give the speaker object the character to show it
                           this.speaker = this.axAgent1.Characters["robby"];
                           this.speaker.Show(0);
                   }
                   catch(FileNotFoundException)   //if the character not found
                   {
                           MessageBox.Show("Invalid charater location");
                   }
           }

In the speak button click function write the following code

C#
private void speak_Click(object sender, System.EventArgs e)
           {
                   if(this.talk.Text != "")
             this.speaker.Speak(this.talk.Text , null);
                   else 
                      this.speaker.Speak("what should i say", null);
           }

In the speaker objects you will find some useful function that help you to control the character such as :

C#
//MoveTo( int x , int y , objectspeed speed);
//Play(string animation);   
// this function make the character to play some animation 
 //The possible animations that the character can play it are
/******************
RestPose, Wave, DontRecognize, Uncertain, Decline,Sad, 
StopListening, GetAttention, GetAttentionReturn,Blink, Idle3_2, 
Surprised,Congratulate_2,Reading,Announce,Read ,ReadReturn,Idle2_2  ,
Writing ,Write ,WriteReturn ,Congratulate ,Confused ,Suggest ,MoveRight, 
MoveLeft,Idle2_1,  MoveUp, MoveDown, 
StartListening, WriteContinued, DoMagic1, 
DoMagic2,Idle1_1,  LookDown, LookDownBlink, LookDownReturn, LookLeft, 
LookLeftBlink, LookLeftReturn,Idle1_3,  LookRight, LookRightBlink, 
LookRightReturn, LookUp, LookUpBlink,LookUpReturn,Idle1_2,
ReadContinued, Pleased, GetAttentionContinued, Process, Search, 
Think,Idle1_4,Greet,Idle3_1,GestureUp,GestureDown,GestureLeft,
GestureRight,Show,Hide,Hearing_4,Hearing_1,Hearing_2,Hearin,
Alert,Explain,Processing,Thinking,Searching,Acknowledge
*********************/

The full code

C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
 
namespace WindowsApplication1
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
           private AxAgentObjects.AxAgent axAgent1;
           private AgentObjects.IAgentCtlCharacter speaker;
           private System.Windows.Forms.Button speak;
           private System.Windows.Forms.RichTextBox talk;
           /// <summary>
           /// Required designer variable.
           /// </summary>
        private System.ComponentModel.Container components = null;
 
           public Form1()
           {
                   //
                   // Required for Windows Form Designer support
                   //
                   InitializeComponent();
 
                   //
                   // TODO: Add any constructor code after 
                   // InitializeComponent call
                   //
           }
 
        /// <summary>
           /// Clean up any resources being used.
           /// </summary>
           protected override void Dispose( bool disposing )
           {
                   if( disposing )
                   {
                           if (components != null) 
                           {
                                  components.Dispose();
                           }
                   }
                   base.Dispose( disposing );
           }
 
           #region Windows Form Designer generated code
           /// <summary>
           /// Required method for Designer support - do not modify
           /// the contents of this method with the code editor.
           /// </summary>
           private void InitializeComponent()
           {
                   System.Resources.ResourceManager resources = 
              new System.Resources.ResourceManager(typeof(Form1));
                   this.axAgent1 = new AxAgentObjects.AxAgent();
                   this.speak = new System.Windows.Forms.Button();
                   this.talk = new System.Windows.Forms.RichTextBox();
         ((System.ComponentModel.ISupportInitialize)
                 (this.axAgent1)).BeginInit();
                   this.SuspendLayout();
                   // 
                   // axAgent1
                   // 
                   this.axAgent1.Enabled = true;
                   this.axAgent1.Location = new System.Drawing.Point(0, 232);
                   this.axAgent1.Name = "axAgent1";
                   this.axAgent1.OcxState = 
                       ((System.Windows.Forms.AxHost.State)
                        (resources.GetObject("axAgent1.OcxState")));
                   this.axAgent1.Size = new System.Drawing.Size(32, 32);
                   this.axAgent1.TabIndex = 0;
                   // 
                   // speak
                   // 
                   this.speak.Location = new System.Drawing.Point(192, 176);
                   this.speak.Name = "speak";
                   this.speak.Size = new System.Drawing.Size(168, 48);
                   this.speak.TabIndex = 1;
                   this.speak.Text = "speak";
                   this.speak.Click += 
                     new System.EventHandler(this.speak_Click);
                   // 
                   // talk
                   // 
                   this.talk.Location = new System.Drawing.Point(24, 24);
                   this.talk.Name = "talk";
                   this.talk.Size = new System.Drawing.Size(328, 136);
                   this.talk.TabIndex = 2;
                   this.talk.Text = "";
                   // 
                   // Form1
                   // 
                   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                   this.ClientSize = new System.Drawing.Size(376, 270);
                   this.Controls.AddRange(new System.Windows.Forms.Control[] {
                         this.talk,
                         this.speak,
                         this.axAgent1});
                   this.Name = "Form1";
                   this.Text = "Form1";
                   this.Load += new System.EventHandler(this.Form1_Load);
                   ((System.ComponentModel.ISupportInitialize)
                         (this.axAgent1)).EndInit();
                   this.ResumeLayout(false);
 
           }
           #endregion
 
           /// <summary>
           /// The main entry point for the application.
           /// </summary>
           [STAThread]
           static void Main() 
           {
                   Application.Run(new Form1());
           }
 
           private void Form1_Load(object sender, System.EventArgs e)
           {
                   try
                   {                          
                    this.axAgent1.Characters.Load("Robby" , "robby.acs");    
//load the character  in the axAgent1 object  
// -- axAgent1 can load more than one character
 
                 this.speaker = this.axAgent1.Characters["robby"];     
                     //give the speaker object the character to show it
                           this.speaker.Show(0);
                   }
         catch(FileNotFoundException)   //if the charater not found  
                       // using IO 
                   {
                           MessageBox.Show("Invalid charater location");
                   }
           }
 
           private void speak_Click(object sender, System.EventArgs e)
           {
                   if(this.talk.Text != "")
             this.speaker.Speak(this.talk.Text , null);
 
                   else 
                           this.speaker.Speak("what should i say", null);
 
           }
    }
}

Tip

You should download the TTS “text-to-speech ” engine on your computer to make the character talk. To download the engine see

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
Ahmed J. Kattan Bachelor degree from Jordan University of Science and Technology computer science department, Master Degree from University of Essex and PhD student at University of Essex ”United Kingdom”, I have written several applications, designed multiple algorithms and several publications. My favorite languages are C++ and C#.



see www.ahmedkattan.com to view Ahmed Kattan's online CV.

Comments and Discussions

 
Questionagent Pin
Member 1275609123-Sep-16 5:30
Member 1275609123-Sep-16 5:30 
QuestionHow to use this Microsoft Agent in Visual Studio 2010... Pin
Aman Raikwar13-Apr-13 9:37
Aman Raikwar13-Apr-13 9:37 
Questionerror Pin
RINKU GOEL30-Mar-13 1:44
RINKU GOEL30-Mar-13 1:44 
Questionquery Pin
amy.shah31-Mar-12 8:05
amy.shah31-Mar-12 8:05 
Generalthere is no sound generated Pin
akramahmed7-May-10 10:01
akramahmed7-May-10 10:01 
GeneralPossible to import this microsoft agent in oracle form6i Pin
mahlkita1-Jul-07 23:01
mahlkita1-Jul-07 23:01 
Questionhow i can control the speed of talk for character? Pin
walalno200529-May-07 9:13
walalno200529-May-07 9:13 
AnswerRe: how i can control the speed of talk for character? Pin
Ahmed jamil Kattan29-May-07 10:21
Ahmed jamil Kattan29-May-07 10:21 
GeneralRe: how i can control the speed of talk for character? Pin
walalno200529-May-07 10:29
walalno200529-May-07 10:29 
QuestionPrevent agent character from user mouse drags Pin
Vineel K19-Sep-06 9:34
Vineel K19-Sep-06 9:34 
GeneralError : Invalid charactor location Pin
muralkara10-May-06 0:29
muralkara10-May-06 0:29 
GeneralRe: Error : Invalid charactor location Pin
Ahmed jamil Kattan13-May-06 6:09
Ahmed jamil Kattan13-May-06 6:09 
GeneralRe: Error : Invalid charactor location Pin
akramahmed7-May-10 9:39
akramahmed7-May-10 9:39 
QuestionHow to use Microsoft Agent in C# on internet Pin
heer coot23-Dec-05 9:32
heer coot23-Dec-05 9:32 
AnswerRe: How to use Microsoft Agent in C# on internet Pin
Ahmed jamil Kattan23-Dec-05 13:04
Ahmed jamil Kattan23-Dec-05 13:04 
GeneralError in some Windows Xp OS Pin
Member 212859128-Jul-05 0:06
Member 212859128-Jul-05 0:06 
GeneralRe: Error in some Windows Xp OS Pin
Ahmed jamil Kattan28-Jul-05 0:26
Ahmed jamil Kattan28-Jul-05 0:26 
GeneralRe: Error in some Windows Xp OS Pin
Member 21285918-Aug-05 16:13
Member 21285918-Aug-05 16:13 
GeneralRe: Error in some Windows Xp OS Pin
Ahmed jamil Kattan8-Aug-05 21:44
Ahmed jamil Kattan8-Aug-05 21:44 
GeneralRe: Error in some Windows Xp OS Pin
Member 21285918-Aug-05 22:03
Member 21285918-Aug-05 22:03 
GeneralError - Please guide Pin
Arindamkundu13-Jul-05 14:38
Arindamkundu13-Jul-05 14:38 
GeneralRe: Error - Please guide Pin
Ahmed jamil Kattan13-Jul-05 20:42
Ahmed jamil Kattan13-Jul-05 20:42 
GeneralRe: Error - Please guide Pin
Anonymous14-Jul-05 2:23
Anonymous14-Jul-05 2:23 
GeneralPlay a Agent Pin
Amir Jalaly26-Jan-05 0:33
Amir Jalaly26-Jan-05 0:33 
GeneralRe: Play a Agent Pin
Ahmed jamil Kattan26-Jan-05 3:25
Ahmed jamil Kattan26-Jan-05 3:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.