熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

在C#下用Microsoft Agent創建超酷語言精靈

2022-06-13   來源: .NET編程 

  一 概述

  微軟的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平台下的開發是相當迅速而有效的其實上面的例子還可以大大擴充使之實現像電子小說閱讀英文聽力測試英文單詞學習等功能讀者不妨一試


From:http://tw.wingwit.com/Article/program/net/201311/13032.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.