设为首页收藏本站language 语言切换
查看: 1677|回复: 0
收起左侧

Visual C#:学做英文朗读器

[复制链接]
发表于 2010-2-25 10:27:40 | 显示全部楼层 |阅读模式
<p >时下学习英文变得越来越重要了,广大编程爱好者中也肯定有很多人要学习英文吧。学习英文的一个重要的因素就是语言环境,然而大家在学习英文的过程中就是缺少了这个环境,毕竟我们在听说方面是缺少条件的。不过现在好了,本文我就将向大家介绍如何自己动手做一款精致的英文朗读软件,而且该软件还带了一个十分有趣的精灵(就和MS Office 2000办公软件中的一样),它能一句一句的给你朗读英文,还是原滋原味的美国英语呢。这样,你还愁学不好英文吗?<p >本文我将使用微软公司的新的RAD开发工具-Visual C#向大家介绍该实例。在程序中,我们需要用到微软的一项Agent技术,通过运用该技术我们就可以在自己的程序中使用很有趣的精灵了:让它给我们朗读英文!<p ><b>系统要求</b><p >· 微软公司视窗2000服务器版或视窗 XP 版<p >· .Net Framewrok和Visual Studio.net正式版<p >· Microsoft  Agent核心组建<p >· Microsoft  Agent的四个精灵:吉尼(Genie)、么林(Merlin)、罗比(Robby)以及皮蒂(Peedy)<p >· 至少有一个英语的Text-to-Speech引擎(现在尚无中文的引擎)<p >· 微软运行时发音API4.0a<p >(如果还要实现语音识别功能的话,还要有微软的语音识别引擎)<p >以上的Microsoft  Agent核心组建、Microsoft  Agent的四个精灵、英语的Text-to-Speech引擎和微软运行时发音API4.0a可以在http://microsoft.com/msagent/downloads.htm下载,要完成此程序我们的电脑必须具有以上的东西,如果没有的话,不妨去下载吧(呵呵,为了学好英文,有什么不舍得的?)。另外,必须要安装Office2000(Office97是不行的)。<p ><center><font color="#000099"><strong>设计界面</strong></font></center><p >首先,打开Visual Studio.net开发工具,新建一个项目,在项目类型里选择“Visual C#项目”,在模板中选择“Windows 应用程序”即可,同时不妨命名为“ReadForMe”,图示如下:<p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_uxz03076447.jpg"></center><p ><center>图1</center><p >在创建程序的用户界面以前我们必须先导入Agent Control这个ActiveX控件,该控件在你安装好系统要求中的项后便有了。下面是导入的方法:选择菜单"工具->自定义工具箱",并选择Microsoft Agent Control 2.0组件,图示如下:<p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_g0h08776448.jpg"></center><p ><center>图2</center><p >在将Agent Control导入到工具箱中后,我们就可以开始设计程序的界面了。首先将一个Agent Control控件拖放到程序的主窗体上,值得说明的是:在程序运行时是看不到窗体上的Agent控件的,只有在设计界面时它才能显示出来。接下来,给程序的主窗体上添加如下控件,并设置相应的属性:<p ><p ><center><ccid_nobr><table border="1" width="502" class="content"><tr><td colspan="2">label1</td><td>Text属性</td><td>选择精灵:</td></tr><tr><td>TextAlign属性</td><td>MiddleRight</td></tr><tr><td>comboBox1</td><td>Items属性</td><td>如图(3)所示</td></tr><tr><td>textBox1</td><td>MultiLine属性</td><td>True</td></tr><tr><td>button1</td><td>Text属性</td><td>导入精灵</td></tr><tr><td>button2</td><td>Text属性</td><td>隐藏精灵</td></tr><tr><td>button3</td><td>Text属性</td><td>朗读英文</td></tr></table></ccid_nobr></center><p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_czj1v276449.jpg"></center><p ><center>图3</center><p >最终我们设计的主界面的图示如下:<p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_fi1swh76450.jpg"></center><p ><center>图4</center><p ><center><font color="#000099"><strong>编写代码</strong></font></center><p >简单的用户界面已经完成,现在我们来进行代码部分的工作:<p >首先,添加using AgentObjects;到代码的开始处。其次,在我们的类里添加私有数据成员:private IAgentCtlCharacterEx Character;(这就是我们要用到的精灵的对象)。修改构造函数如下:<p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>public Form1(){//// Windows 窗体设计器支持所必需的//InitializeComponent();button2.Enabled=false;//先使下面的两个按钮无效button3.Enabled=false;}</ccid_code></pre></td></tr></table></ccid_nobr><p >接着就是编写按钮控件的消息相应代码了,下面我给出了完整的代码,其中按钮的消息相应代码以加粗的字体给出,以示区别:<p ><ccid_nobr><table width="550" border="1" cellspacing="0" cellpadding="2" bordercolorlight = "black" bordercolordark = "#FFFFFF" align="center"><tr><td bgcolor="e6e6e6" class="code"><pre><ccid_code>using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using AgentObjects;namespace ReadForYou{        /// &lt;summary&gt;        /// Form1 的摘要说明。        /// &lt;/summary&gt;        public class Form1 : System.Windows.Forms.Form        {                private AxAgentObjects.AxAgent axAgent1;                private System.Windows.Forms.Button button1;                private System.Windows.Forms.Button button2;                private System.Windows.Forms.Button button3;                private System.Windows.Forms.TextBox textBox1;                private System.Windows.Forms.Label label1;                private System.Windows.Forms.ComboBox comboBox1;                private IAgentCtlCharacterEx Character;                /// &lt;summary&gt;                /// 必需的设计器变量。                /// &lt;/summary&gt;                private System.ComponentModel.Container components = null;                public Form1()                {                        //                        // Windows 窗体设计器支持所必需的                        //                        InitializeComponent();                        button2.Enabled=false;//先使下面的两个按钮无效                        button3.Enabled=false;                }                /// &lt;summary&gt;                /// 清理所有正在使用的资源。                /// &lt;/summary&gt;                protected override void Dispose( bool disposing )                {                        if( disposing )                        {                                if (components != null)                                 {                                        components.Dispose();                                }                        }                        base.Dispose( disposing );                }                #region Windows Form Designer generated code                /// &lt;summary&gt;                /// 设计器支持所需的方法 - 不要使用代码编辑器修改                /// 此方法的内容。                /// &lt;/summary&gt;                private void InitializeComponent()                {                        System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));                        this.axAgent1 = new AxAgentObjects.AxAgent();                        this.button1 = new System.Windows.Forms.Button();                        this.button2 = new System.Windows.Forms.Button();                        this.button3 = new System.Windows.Forms.Button();                        this.textBox1 = new System.Windows.Forms.TextBox();                        this.label1 = new System.Windows.Forms.Label();                        this.comboBox1 = new System.Windows.Forms.ComboBox();((System.ComponentModel.ISupportInitialize)(this.axAgent1)).BeginInit();                        this.SuspendLayout();                        //                         // axAgent1                        //                         this.axAgent1.Enabled = true;                this.axAgent1.Location = new System.Drawing.Point(32, 56);                        this.axAgent1.Name = &quot;axAgent1&quot;;                        this.axAgent1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject(&quot;axAgent1.OcxState&quot;)));                        this.axAgent1.Size = new System.Drawing.Size(32, 32);                        this.axAgent1.TabIndex = 0;                        //                         // button1                        //                         this.button1.Location = new System.Drawing.Point(8, 256);                        this.button1.Name = &quot;button1&quot;;                        this.button1.TabIndex = 1;                        this.button1.Text = &quot;导入精灵&quot;;                        this.button1.Click += new System.EventHandler(this.button1_Click);                        //                         // button2                        //                         this.button2.Location = new System.Drawing.Point(112, 256);                        this.button2.Name = &quot;button2&quot;;                        this.button2.TabIndex = 2;                        this.button2.Text = &quot;隐藏精灵&quot;;                        this.button2.Click += new System.EventHandler(this.button2_Click);                        //                         // button3                        //                         this.button3.Location = new System.Drawing.Point(216, 256);                        this.button3.Name = &quot;button3&quot;;                        this.button3.TabIndex = 3;                        this.button3.Text = &quot;朗读英文&quot;;                        this.button3.Click += new System.EventHandler(this.button3_Click);                        //                         // textBox1                        //                         this.textBox1.Location = new System.Drawing.Point(16, 48);                        this.textBox1.Multiline = true;                        this.textBox1.Name = &quot;textBox1&quot;;                        this.textBox1.Size = new System.Drawing.Size(272, 184);                        this.textBox1.TabIndex = 4;                        this.textBox1.Text = &quot;&quot;;                        //                         // label1                        //                         this.label1.Location = new System.Drawing.Point(24, 16);                        this.label1.Name = &quot;label1&quot;;                        this.label1.TabIndex = 5;                        this.label1.Text = &quot;选择精灵:&quot;;                        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;                        //                         // comboBox1                        //                         this.comboBox1.Items.AddRange(new object[] { &quot;吉尼(Genie)&quot;,   &quot;么林(Merlin)&quot;,   &quot;罗比(Robby)&quot;,   &quot;皮蒂(Peedy)&quot;});        this.comboBox1.Location = new System.Drawing.Point(152, 16);                        this.comboBox1.Name = &quot;comboBox1&quot;;                        this.comboBox1.Size = new System.Drawing.Size(121, 20);                        this.comboBox1.TabIndex = 6;                        //                         // Form1                        //                         this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);                        this.ClientSize = new System.Drawing.Size(304, 293);        this.Controls.AddRange(new System.Windows.Forms.Control[] { this.comboBox1,this.label1,  this.textBox1,  this.button3,          this.button2, this.button1, this.axAgent1});                        this.MaximizeBox = false;                        this.Name = &quot;Form1&quot;;                        this.Text = &quot;ReadForMe&quot;;((System.ComponentModel.ISupportInitialize)(this.axAgent1)).EndInit();                        this.ResumeLayout(false);                }                #endregion                /// &lt;summary&gt;                /// 应用程序的主入口点。                /// &lt;/summary&gt;                [STAThread]                static void Main()                 {                        Application.Run(new Form1());                }                private void button1_Click(object sender, System.EventArgs e)                {                        if( this.comboBox1.Text.Length == 0 )                        {                                MessageBox.Show( &quot;请先选择一个精灵!&quot;, &quot;错误信息&quot; );                                return;                        }                        switch( this.comboBox1.Text )                        {                                case &quot;吉尼(Genie)&quot;:                axAgent1.Characters.Load(&quot;Genie&quot;, (object)&quot;GENIE.ACS&quot;);//导入精灵吉尼                                        Character = axAgent1.Characters[&quot;Genie&quot;];                        Character.LanguageID = 0x409;//把语言设置为英语,这里不能是中文                        Character.Show(null);//显示精灵                                        break;                                case &quot;么林(Merlin)&quot;:                        axAgent1.Characters.Load(&quot;Merlin&quot;, (object)&quot;MERLIN.ACS&quot;);//导入精灵么林                                        Character = axAgent1.Characters[&quot;Merlin&quot;];                                        Character.LanguageID = 0x409;//把语言设置为英语,这里不能是中文                                        Character.Show(null);//显示精灵                                                break;                        case &quot;罗比(Robby)&quot;:                        axAgent1.Characters.Load(&quot;Robby&quot;, (object)&quot;ROBBY.ACS&quot;);//导入精灵罗比                                        Character = axAgent1.Characters[&quot;Robby&quot;];                                        Character.LanguageID = 0x409;//把语言设置为英语,这里不能是中文                                        Character.Show(null);//显示精灵                                                break;                                case &quot;皮蒂(Peedy)&quot;:                                        axAgent1.Characters.Load(&quoteedy&quot;, (object)&quotEEDY.ACS&quot;);//导入精灵皮蒂                                        Character = axAgent1.Characters[&quoteedy&quot;];                                        Character.LanguageID = 0x409;//把语言设置为英语,这里不能是中文                                        Character.Show(null);//显示精灵                                                break;                        }                        button1.Enabled=false;//重新设置按钮的有效性                        button2.Enabled=true;                        button3.Enabled=true;                }                private void button2_Click(object sender, System.EventArgs e)                {                        Character.Play(&quot;Wave&quot;);                        Character.Play(&quot;Hide&quot;);//隐藏精灵                        switch( this.comboBox1.Text )                        {                                case &quot;吉尼(Genie)&quot;:                                        axAgent1.Characters.Unload(&quot;Genie&quot;);                                        break;                                case &quot;么林(Merlin)&quot;:                                        axAgent1.Characters.Unload(&quot;Merlin&quot;);                                        break;                                case &quot;罗比(Robby)&quot;:                                        axAgent1.Characters.Unload(&quot;Robby&quot;);                                        break;                                case &quot;皮蒂(Peedy)&quot;:                                        axAgent1.Characters.Unload(&quoteedy&quot;);                                break;                        }                        button1.Enabled=true;                        button2.Enabled=false;//使下面的两个按钮无效                        button3.Enabled=false;                }                private void button3_Click(object sender, System.EventArgs e)                {                        if(textBox1.Text.Length == 0)  //如果没有字符的话,就不读                        return;                                                      Character.Speak(textBox1.Text, null);//让精灵朗读文本                }        }}</ccid_code></pre></td></tr></table></ccid_nobr><p >这样,我们的英文朗读软件就完成了,下面按下Ctrl+F5试试效果吧,图示如下,呵呵,还不错吧:)<p ><center><img  src="http://www.hh010.com/upload_files/article/244/9_smv6bu76451.jpg"></center><p ><center>图5</center><p ><center><font color="#000099"><strong>全文总结</strong></font></center><p >到此,我们的带精灵的英文朗读器就开发完毕了。通过它,我们可以锻炼我们的英文听力水平啦。不过Agent技术有它更广泛的应用,比如我们可以扩展该实例,使得它具有语音识别能力。那样的话,我们不仅可以通过它来提高英文的听力水平,更能用它来锻炼我们的口语能力了。如果你想做的话,就赶快动手吧!<p >(责任编辑 <ccid_nobr>Sunny</ccid_nobr>)                                 <p align="center"></p></p>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-10 13:15 , Processed in 0.071760 second(s), 24 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表