一 概述
微软的Office中用到了一些被称为办公助手(Office Assistance)的精灵来给用户提供帮助这样做的效果是显而易见的大家可以得到很有效的帮助并且使用户界面显得非常友好现在我们只要使用Microsoft Agent(基于COM)我们就可以在自己的程序中使用这种精灵来给程序增光添彩用这种精灵我们可以实现语音的朗读表演动画甚至还可以实现语音识别呢!
二 要求
()微软公司视窗服务器版或视窗 XP 版
()Net FrameWrok SDK Beta 版
()Microsoft Agent核心组建
()Microsoft Agent的精灵吉尼(Genie)么林(Merlin)罗比(Robby)和皮蒂(Peedy)
()至少有一个英语的TexttoSpeech引擎(现在还找不到中文的)
()微软运行时发音APIa
如果还要实现语音识别功能的话还要有微软的语音识别引擎所有这些都可以在下载另外必须要安装Office(Office是不行的)
三 实现方法
.打开VSNet新建一个工程不妨取名为CoolUI图示如下
.创建用户界面
选择菜单工具>自定义工具箱并选择Microsoft Agent Control 组件图示
将Microsoft Agent Control控件添加到窗体上(在程序运行时是看不到窗体是的Microsoft Agent控件的只有在设计界面时它才显示出来)并课设计窗体如下
将主窗体的Text属性设置为CoolUI将左边三个按钮的Text属性分别设置为导入精灵朗读文本隐藏精灵将textBox的Text属性设置为Type anything here for the character to read for you!(Only English)Multiline属性设置为True
.简单的用户界面已经完成现在我们来进行代码部分的工作
首先添加using AgentObjects;到代码的开始处其次在我们的类里添加私有数据成员private IAgentCtlCharacterEx Character;(这就是我们要用到的精灵的对象)修改构造函数如下
public Form()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
buttonEnabled=false;//先使下面的两个按钮无效
buttonEnabled=false;
//
// TODO: Add any constructor code after InitializeComponent call
//
}
接着添加左边三个按钮的鼠标单击的消息相应函数
private void button_Click(object sender SystemEventArgs e)
private void button_Click(object sender SystemEventArgs e)
private void button_Click(object sender SystemEventArgs e)
代码如下
private void button_Click(object sender SystemEventArgs e)
{
axAgentCharactersLoad(Genie (object)GENIEACS);//导入吉尼这个精灵
Character = axAgentCharacters[Genie];
CharacterLanguageID = x;//把语言设置为英语这里不能是中文
CharacterShow(null);//显示精灵
buttonEnabled=false;//重新设置按钮的有效性
buttonEnabled=true;
buttonEnabled=true;
}
private void button_Click(object sender SystemEventArgs e)
{
if(textBoxTextLength == ) //如果没有字符的话就不读
return;
CharacterSpeak(textBoxText null);//让精灵朗读文本
}
private void button_Click(object sender SystemEventArgs e)
{
CharacterPlay(Wave);
CharacterPlay(Hide);//隐藏精灵
}
所有完整的代码如下
using System;
using SystemDrawing;
using SystemCollections;
using SystemComponentModel;
using SystemWindowsForms;
using SystemData;
using AgentObjects;
namespace CoolUI
{
///
/// Summary description for Form
///
public class Form : SystemWindowsFormsForm
{
private AxAgentObjectsAxAgent axAgent;
private IAgentCtlCharacterEx Character;
private SystemWindowsFormsTextBox textBox;
private SystemWindowsFormsButton button;
private SystemWindowsFormsButton button;
private SystemWindowsFormsButton button;
///
/// Required designer variable
///
private SystemComponentModelContainer components = null;
public Form()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
buttonEnabled=false;//先使下面的两个按钮无效
buttonEnabled=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)
{
componentsDispose();
}
}
baseDispose( 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()
{
SystemResourcesResourceManager resources = new SystemResourcesResourceManager(typeof(Form));
thistextBox = new SystemWindowsFormsTextBox();
thisbutton = new SystemWindowsFormsButton();
thisbutton = new SystemWindowsFormsButton();
thisbutton = new SystemWindowsFormsButton();
thisaxAgent = new AxAgentObjectsAxAgent();
((SystemComponentModelISupportInitialize)(thisaxAgent))BeginInit();
thisSuspendLayout();
//
// textBox
//
thistextBoxLocation = new SystemDrawingPoint( );
thistextBoxMultiline = true;
thistextBoxName = textBox;
thistextBoxSize = new SystemDrawingSize( );
thistextBoxTabIndex = ;
thistextBoxText = Type anything here for the character to read for you!(Only English);
//
// button
//
thisbuttonLocation = new SystemDrawingPoint( );
thisbuttonName = button;
thisbuttonTabIndex = ;
thisbuttonText = 导入精灵;
thisbuttonClick += new SystemEventHandler(thisbutton_Click);
//
// button
//
thisbuttonLocation = new SystemDrawingPoint( );
thisbuttonName = button;
thisbuttonTabIndex = ;
thisbuttonText = 朗读文本;
thisbuttonClick += new SystemEventHandler(thisbutton_Click);
//
// button
//
thisbuttonLocation = new SystemDrawingPoint( );
thisbuttonName = button;
thisbuttonTabIndex = ;
thisbuttonText = 隐藏精灵;
thisbuttonClick += new SystemEventHandler(thisbutton_Click);
//
// axAgent
//
thisaxAgentEnabled = true;
thisaxAgentLocation = new SystemDrawingPoint( );
thisaxAgentName = axAgent;
thisaxAgentOcxState = ((SystemWindowsFormsAxHostState)(resourcesGetObject(axAgentOcxState)));
thisaxAgentSize = new SystemDrawingSize( );
thisaxAgentTabIndex = ;
//
// Form
//
thisAutoScaleBaseSize = new SystemDrawingSize( );
thisClientSize = new SystemDrawingSize( );
thisControlsAddRange(new SystemWindowsFormsControl[] {
thisbutton
thisbutton
thistextBox
thisbutton
thisaxAgent});
thisName = Form;
thisText = CoolUI;
((SystemComponentModelISupportInitialize)(thisaxAgent))EndInit();
thisResumeLayout(false);
}
#endregion
///
/// The main entry point for the application
///
[STAThread]
static void Main()
{
ApplicationRun(new Form());
}
private void button_Click(object sender SystemEventArgs e)
{
axAgentCharactersLoad(Genie (object)GENIEACS);//导入吉尼这个精灵
Character = axAgentCharacters[Genie];
CharacterLanguageID = x;//把语言设置为英语这里不能是中文
CharacterShow(null);//显示精灵
buttonEnabled=false;//重新设置按钮的有效性
buttonEnabled=true;
buttonEnabled=true;
}
private void button_Click(object sender SystemEventArgs e)
{
if(textBoxTextLength == ) //如果没有字符的话就不读
return;
CharacterSpeak(textBoxText null);//让精灵朗读文本
}
private void button_Click(object sender SystemEventArgs e)
{
CharacterPlay(Wave);
CharacterPlay(Hide);//隐藏精灵
} }
}
.好了现在完成了所有的工作了安Ctrl+F试试效果吧!
四.总结
从以上的例子我们可以发现用C#做Windows平台下的开发是相当迅速而有效的其实上面的例子还可以大大扩充使之实现像电子小说阅读英文听力测试英文单词学习等功能读者不妨一试