程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> 在C#下用Microsoft Agent創建非常棒語言精靈(1)

在C#下用Microsoft Agent創建非常棒語言精靈(1)

編輯:C#基礎知識

本文就通過介紹利用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 );




  

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




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved