在.Net架構中使用微軟AGENT
微軟Agent不是以ActiveX控制DLL的形式提供的。要在.NET中使用它,可以使用由.Net框架SDK提供的AxImp.exe工具:
AxImp -->> ActiveX控制-Win窗體組合體生成器
Syntax: AxImp [/? | [[/source] OCXName]]
Aximp agentctl.dll
上面的命令生成二個文件:AxAgentObjects.dll和AgentObjects.dll。通過使用這二個文件,我們就可以在.Net中使用Agent了。
C#中的微軟Agent:
要在C#中使用微軟Agent,我們必須在程序中添加二個DLL文件:AxAgentObjects.dll和AgentObjects.dll。加載動畫角色的代碼是相當簡單的:
AxAgent.Characters.Load("Genie",(object)"C:/Windows/Msagent/chars/GENIE.acs");
Character = AxAgent.Characters["GenIE"];
file://將語言設置為美國英語
Character.LanguageID = 0x409;
file://該行代碼顯示角色
Character.Show(null);
讓角色說話的代碼如下所示:
Character.Speak ("Welcome you sir VISIT www.onlinecsharpteach.Netfirms.com ",null);
下面我們來看一個例子:
The Example:
using System;
using System.Drawing;
using System.WinForms;
using AgentObjects;
public class Hello: Form
{
private System.ComponentModel.Container components;
private System.WinForms.Button button2;
private System.WinForms.Button button1;
private System.WinForms.TextBox textBox1;
private AxAgentObjects.AxAgent AxAgent;
private IAgentCtlCharacterEx Character;
public Hello()
{
InitializeComponent();
}
public static void Main(string[] args)
{
Application.Run(new Hello());
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.WinForms.Button();
this.button2 = new System.WinForms.Button();
this.textBox1 = new System.WinForms.TextBox();
this.AxAgent = new AxAgentObjects.AxAgent();
AxAgent.BeginInit();
button2.Click += new System.EventHandler(button2_Click);
button1.Location = new System.Drawing.Point(88, 208);
button1.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)128, (byte)128);
button1.Size = new System.Drawing.Size(152, 32);
button1.TabIndex = 1;
button1.Text = "Load character";
button2.Location = new System.Drawing.Point(120, 240);
button2.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)128, (byte)128);
button2.Size = new System.Drawing.Size(96, 24);
button2.TabIndex = 2;
button2.Text = "SPEAK";
textBox1.Location = new System.Drawing.Point(48, 8);
textBox1.Text = " ";
textBox1.Multiline = true;
textBox1.TabIndex = 0;
textBox1.Size = new System.Drawing.Size(248, 200);
textBox1.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)128, (byte)128);
this.Text = "MSAGENT DEMO";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.WindowState = System.WinForms.FormWindowstate.Maximized;
this.BackColor =
(System.Drawing.Color)System.Drawing.Color.FromARGB((byte)255, (byte)192, (byte)192);
this.ClIEntSize = new System.Drawing.Size(344, 301);
this.Controls.Add(button2);
this.Controls.Add(button1);
this.Controls.Add(textBox1);
this.Controls.Add(AxAgent);
button1.Click += new System.EventHandler(button1_Click);
AxAgent.EndInit();
}
protected void button2_Click(object sender, System.EventArgs e)
{
if(textBox1.Text.Length == 0)
return;
Character.Speak(textBox1.Text, null);
}
protected void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.AddExtension = true;
openFileDialog.Filter = "Microsoft Agent Characters (*.acs)|*.acs";
openFileDialog.FilterIndex = 1 ;
openFileDialog.RestoreDirectory = true ;
if(openFileDialog.ShowDialog() != DialogResult.OK)
return;
try { AxAgent.Characters.Unload("CharacterID"); }
catch { }
AxAgent.Characters.Load("CharacterID", (object)openFileDialog.FileName);
Character = AxAgent.Characters["CharacterID"];
Character.LanguageID = 0x409;
Character.Show(null);
Character.Play ("announce");
Character.Speak ("welcome you sir",null);
}
}
輸出:
結束語:
微軟的Agent API提供了支持動畫角色顯示的服務,被配置為OLE Automation(COM)服務器時,它能夠使多個被稱為客戶或客戶端應用軟件的應用程序同時托管或使用其動畫、輸入、輸出服務。