在C#下用Microsoft Agent创建超酷语言精灵(1)

上一篇 / 下一篇  2008-04-22 21:08:10 / 个人分类:技术文章

本文就通过介绍利用Microsoft Agent来创建一个超酷用户界面(就像Office2000那种办公助手的界面,并有语音朗读功能)来向大家展示一下用C#进行Windows应用程序快速开发的优点。

一 概述

微软的Office2000中用到了一些被称为“办公助手”(Office Assistance)的精灵来给用户提供帮助,这样做的效果是显而易见的,大家可以得到很有效的帮助并且使用户界面显得非常友好。现在,我们只要使用 Microsoft Agent(基于COM),我们就可以在自己的程序中使用这种精灵来给程序增光添彩。用这种精灵,我们可以实现语音的朗读、表演动画甚至还可以实现语音识 别呢! 

二 要求

(1)微软公司视窗2000服务器版或视窗 XP 版
(2).Net FrameWrok SDK Beta 2版
(3)Microsoft Agent核心组建
(4)Microsoft Agent的精灵:吉尼(Genie)、么林(Merlin)、罗比(Robby)和皮蒂(Peedy)
(5)至少有一个英语的Text-to-Speech引擎(现在还找不到中文的)
(6)微软运行时发音API4.0a

如果还要实现语音识别功能的话,还要有微软的语音识别引擎,所有这些都可以在http://microsoft.com/msagent/downloads.htm下载。另外,必须要安装Office2000(Office97是不行的)。 

三 实现方法

1.打开VS.Net,新建一个工程,不妨取名为CoolUI。图示如下: 

2.创建用户界面。

选择菜单:工具->自定义工具箱,并选择Microsoft Agent Control 2.0组件,图示: 

将Microsoft Agent Control控件添加到窗体上(在程序运行时是看不到窗体是的Microsoft Agent控件的,只有在设计界面时它才显示出来),并课设计窗体如下: 

将 主窗体的Text属性设置为“CoolUI”;将左边三个按钮的Text属性分别设置为“导入精灵”、“朗读文本”、“隐藏精灵”;将textBox的 Text属性设置为“Type anything here for the character to read for you!(Only English)”,Multiline属性设置为True。 

3.简单的用户界面已经完成,现在我们来进行代码部分的工作:

首先,添加using AgentObjects;到代码的开始处。其次,在我们的类里添加私有数据成员:private IAgentCtlCharacterEx Character;(这就是我们要用到的精灵的对象)。修改构造函数如下:

public Form1()
{
//
// Required for Windows Form. Designer support
//
InitializeComponent();
button2.Enabled=false;//先使下面的两个按钮无效
button3.Enabled=false;

//
// TODO: Add any constructor code after InitializeComponent call
//
}
接着,添加左边三个按钮的鼠标单击的消息相应函数:
private void button1_Click(object sender, System.EventArgs e)
private void button2_Click(object sender, System.EventArgs e)
private void button3_Click(object sender, System.EventArgs e)
代码如下:
private void button1_Click(object sender, System.EventArgs e)
{
axAgent1.Characters.Load("Genie", (object)"GENIE.ACS");//导入吉尼这个精灵
Character = axAgent1.Characters["Genie"];
Character.LanguageID = 0x409;//把语言设置为英语,这里不能是中文

Character.Show(null);//显示精灵

button1.Enabled=false;//重新设置按钮的有效性
button2.Enabled=true;
button3.Enabled=true;
}

private void button2_Click(object sender, System.EventArgs e)
{
if(textBox1.Text.Length == 0) //如果没有字符的话,就不读
return;

Character.Speak(textBox1.Text, null);//让精灵朗读文本
}

private void button3_Click(object sender, System.EventArgs e)
{
Character.Play("Wave");
Character.Play("Hide");//隐藏精灵
}

所有完整的代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using AgentObjects;

namespace CoolUI
{
///
/// Summary description for Form1.
///

public class Form1 : System.Windows.Forms.Form.
{
private AxAgentObjects.AxAgent axAgent1;
private IAgentCtlCharacterEx Character;

private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form. Designer support
//
InitializeComponent();
button2.Enabled=false;//先使下面的两个按钮无效
button3.Enabled=false;

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-07-09  
  12345
6789101112
13141516171819
20212223242526
2728293031  

数据统计

  • 访问量: 55897
  • 日志数: 24223
  • 建立时间: 2007-12-06
  • 更新时间: 2008-06-15

RSS订阅

Open Toolbar