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:
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
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
namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private AxAgentObjects.AxAgent axAgent1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
Now create object of type AgentObjects.IAgentCtlCharacter
and
name this object speaker ,now at the load function write the following
private void Form1_Load(object sender, System.EventArgs e)
{
try
{
this.axAgent1.Characters.Load("Robby" , "robby.acs");
this.speaker = this.axAgent1.Characters["robby"];
this.speaker.Show(0);
}
catch(FileNotFoundException)
{
MessageBox.Show("Invalid charater location");
}
}
In the speak button click function write the following code
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 :
The full code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace WindowsApplication1
{
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;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
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();
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;
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);
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 = "";
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
[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");
this.speaker = this.axAgent1.Characters["robby"];
this.speaker.Show(0);
}
catch(FileNotFoundException)
{
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