一 概述

  微软的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 );

  }

  #region Windows Form Designer generated code

  ///

  /// Required method for Designer support - do not modify

  /// the contents of this method with the code editor.

  ///

  private void InitializeComponent()

  {

  System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));

  this.textBox1 = new System.Windows.Forms.TextBox();

  this.button1 = new System.Windows.Forms.Button();

  this.button2 = new System.Windows.Forms.Button();

  this.button3 = new System.Windows.Forms.Button();

  this.axAgent1 = new AxAgentObjects.AxAgent();

  ((System.ComponentModel.ISupportInitialize)(this.axAgent1)).BeginInit();

  this.SuspendLayout();

  //

  // textBox1

  //

  this.textBox1.Location = new System.Drawing.Point(112, 24);

  this.textBox1.Multiline = true;

  this.textBox1.Name = "textBox1";

  this.textBox1.Size = new System.Drawing.Size(224, 152);

  this.textBox1.TabIndex = 2;

  this.textBox1.Text = "Type anything here for the character to read for you!(Only English)";

  //

  // button1

  //

  this.button1.Location = new System.Drawing.Point(16, 24);

  this.button1.Name = "button1";

  this.button1.TabIndex = 1;

  this.button1.Text = "导入精灵";

  this.button1.Click += new System.EventHandler(this.button1_Click);

  //

  // button2

  //

  this.button2.Location = new System.Drawing.Point(16, 80);

  this.button2.Name = "button2";

  this.button2.TabIndex = 1;

  this.button2.Text = "朗读文本";

  this.button2.Click += new System.EventHandler(this.button2_Click);

  //

  // button3

  //

  this.button3.Location = new System.Drawing.Point(16, 136);

  this.button3.Name = "button3";

  this.button3.TabIndex = 1;

  this.button3.Text = "隐藏精灵";

  this.button3.Click += new System.EventHandler(this.button3_Click);

  //

  // axAgent1

  //

  this.axAgent1.Enabled = true;

  this.axAgent1.Location = new System.Drawing.Point(320, 176);

  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;

  //

  // Form1

  //

  this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

  this.ClientSize = new System.Drawing.Size(360, 213);

  this.Controls.AddRange(new System.Windows.Forms.Control[] {

  this.button3,

  this.button2,

  this.textBox1,

  this.button1,

  this.axAgent1});

  this.Name = "Form1";

  this.Text = "CoolUI";

  ((System.ComponentModel.ISupportInitialize)(this.axAgent1)).EndInit();

  this.ResumeLayout(false);

  }

  #endregion

  ///

  /// The main entry point for the application.

  ///

  [STAThread]

  static void Main()

  {

  Application.Run(new Form1());

  }

  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");//隐藏精灵

  }   }

  }

  4.好了,现在完成了所有的工作了,安Ctrl+F5试试效果吧!

  四.总结

  从以上的例子,我们可以发现用C#做Windows平台下的开发是相当迅速而有效的。其实上面的例子还可以大大扩充,使之实现像电子小说阅读、英文听力测试、英文单词学习等功能,读者不妨一试。

C#中用Microsoft Agent为自己的程序创建精灵助手相关推荐

  1. Microsoft编写优质无错C程序秘诀

      编程精粹 ───     Microsoft编写优质无错C程序秘诀 Writing Clean Code ───     Microsoft Techniques for Developing B ...

  2. 编程精粹 --Microsoft编写优质无错C程序秘诀

    献给我的妻子Beth, 以及我的双亲Joseph和Julia Maguire ────为了他们的爱和支持 序 1986年,在为几家小公司咨询和工作了10年之后为了获得编写Macintosh应用程序的经 ...

  3. 企业内网中的WSUS更新服务 服务器连接到Microsoft Update来获取更新程序

    这里我们先了解下WSUS更新的途径 1.单WSUS服务器环境 企业网络中部署了一台WSUS服务器,WSUS服务器连接到Microsoft Update来获取更新程序(称之为:同步),并分发给企业网络中 ...

  4. Microsoft Visusl C++2010运行程序时,调试弹出黑框自动闪退无法看见运行结果的解决方法

    1.Microsoft Visusl C++2010运行程序时,调试弹出黑框自动闪退无法看见运行结果的解决方法 方法在图片下面,耐心的看哟,千万不要错过这莫好的方法呢!!! 一个小案例(输入数据转换度 ...

  5. Microsoft Graph Toolkit 新版发布 - 新的 Microsoft Teams 身份验证提供程序和文件上传功能

    微软宣布 Microsoft Graph Toolkit 发布了新的 2.3 版本,这个版本包括一个新的 Microsoft Teams 身份验证提供程序,文件列表 (File List) 组件中的文 ...

  6. windows安装程序创建_如何在Windows上创建已安装程序的列表

    windows安装程序创建 Reinstalling Windows is a good way to fix serious problems with your computer, or just ...

  7. 实验0.2 OpenGL程序创建与运行

    下列介绍与图示均以Microsoft Visual Studio Community 2017版本(下面简称为VS)为例,其它版本类似. 1. 创建控制台应用 (1)点击:文件→新建→项目,如下图所示 ...

  8. 编写程序创建一个通讯录文件,在其中存入10位同学的姓名、年龄、电话号码,并在屏幕上输出第2、4、6、8、10位同学的信息

    <程序设计基础-c语言>杨莉 刘鸿翔 ISBN-978-7-03-032903-5 p257 习题8 8.编写程序创建一个通讯录文件,在其中存入10位同学的姓名.年龄.电话号码,并在屏幕上 ...

  9. 编程第一个Apple Watch程序创建项目

    编程第一个Apple Watch程序创建项目 2.4  编程第一个程序 本节将通过编写第一个程序,为开发者讲解如何添加Watch应用对象.运行程序.界面设计.编写代码等内容本文选自Apple Watc ...

最新文章

  1. Datawhale组队学习周报(第021周)
  2. 再次升级,985博士整理的71个OpenCV实战项目教程开放下载!
  3. android logger的使用
  4. namesapce的作用 增加访问路径 目的:区分不同包的相同action的访问路径
  5. c++ vector拷贝构造_vector------stl学习笔记一
  6. django-阻止某一个IP访问某一个页面
  7. android开发 视图联动_android开发_ViewGroup(组视图)-- 五大布局
  8. andriod studio 启动service失败_惊呆了!女儿拿着小天才电话手表,问我Android启动流程!...
  9. Linux各发行版本 优缺点 简介
  10. python双目视觉三维重建代码_双目立体视觉的三维重建方法与流程
  11. 学python后到底能干什么-学Python后到底能干什么
  12. office怎么像wps一样多栏_时常受到欺负怎么办?——要像对付野狗一样对付坏人!...
  13. java 得到bean的属性_获取javaBean所有属性及类型.doc
  14. 斐波那契数列PHP非递归数组实现
  15. 重新想象 Windows 8 Store Apps (35) - 通知: Toast 详解
  16. java程序员简历范文
  17. 安卓没有删除谷歌服务框架
  18. STM32之贪吃蛇游戏
  19. Debian 12采用 Ubuntu三重缓冲
  20. x-frame-options、iframe与iframe的一些操作

热门文章

  1. 比较两组数据的差异用什么图更直观_扩增子图表解读7三元图:三组差异数量和关系...
  2. Nagios 被动检测oracle Rman备份情况
  3. MFC知识点和常用类控件的使用
  4. windows 修改用户名
  5. linux网口初始化_Linux 初始化系统配置(CentOS 6)
  6. DHTMLXGantt 中使用工作时间日历的示例
  7. python matplotlib 绘制二维数据中某些列到折线图,没有线的解决方法
  8. 去除字符串中的空格(c++)
  9. 第15课:郭盛华课程_VB编程之图形与图像控件的使用方法
  10. 教资公共课考试别慌,这样做很轻松就通关