如何在 C# 中使用 Microsoft Agent






3.21/5 (20投票s)
2004年9月8日
2分钟阅读

142699

4273
一篇关于如何在 C# 应用程序中使用 Microsoft Agent 的文章
引言
Microsoft Agent 是一种用于在 Windows 应用程序或网页中添加交互式动画角色的技术;这些角色根据用户输入通过语音识别引擎进行操作,并且它们还可以通过文本到语音引擎说话并告诉用户一些信息。Microsoft 为程序员提供了四个角色
- A-Peddy
- B-Genie
- C-Merlin
- D-Robby。
每个角色都有自己的一组动画
背景
我搜索了许多网站,寻找可以使用 MS Agent 为最终用户提供帮助的代码。
在搜索 C# 书籍后,我找到了一些不错的代码,帮助我创建了这个简单的应用程序。希望它能作为一个基本的架构提供帮助。
使用代码
首先,您应该简单地打开 VS.NET,然后在文件菜单上单击新建、项目。在“新建项目”对话框中,选择 Windows 应用程序模板项目,并将其命名为WindowsApplication1,如下所示
创建窗口后,添加一个按钮并将其命名为 speak,添加一个 RichTextBox 并将其命名为 talk,您应该添加 Microsoft Agent 组件,为此,单击工具菜单中的自定义工具箱,然后将出现以下对话框
选择 Microsoft Agent 控制 2.0 并单击确定。现在在您的工具箱的末尾,您会发现其中添加了一个新项目“Microsoft Agent”。将此项目拖动并将其放到您的应用程序中,放下“Microsoft Agent”后,.Net 将生成一个类型为AxAgentObjects.AxAgent
的新对象,如下所示
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
//
}
现在创建类型为AgentObjects.IAgentCtlCharacter
的对象,并将其命名为 speaker,现在在 load 函数中写入以下内容
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");
}
}
在 speak 按钮单击函数中写入以下代码
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);
}
在 speaker 对象中,您会发现一些有用的函数可以帮助您控制角色,例如
//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
*********************/
完整的代码
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);
}
}
}
提示
您应该在计算机上下载 TTS(文本到语音)引擎,以使角色说话。要下载该引擎,请参阅