庞大资源库的计算机教程网站!
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
首页 操作系统 程序设计 图形图像 媒体动画 机械电子 WEB开发 数 据 库 办公系列 路由技术 网络原理 网络应用
认证考试 安全技术
首页>程序设计>C#语言>系统编程>正文
资料搜索
Google搜索
Google
返回上级列表

推荐文章

快速保存网页中所有图片的方法
Windows中让光驱巧妙“隐身”技
防范非法用户入侵Win 2000/XP系
两款比较典型的ASP木马防范方法
有关表格边框的css语法整理
Windows XP中可以被禁用的服务
SQL Server导出导入数据方法
Javascript所有对象的属性的获
网页(HTML)中的特殊字符
与篮球共舞,尽显模式本色
QQ病毒的手工清除方法
Photoshop为极品美女打造性感睫
天衣无缝:IIS与PHP水火也相容
SQL Server存储过程编写和优化

在C#应用程序中控制输入法

 作者:周毅    日期:2005-8-8 9:55:02
字号选择〖 〗/ 双击滚屏 单击停止   
在Windows系统一般都安装了至少三种输入法,在输入数据时常常会切换输入法,虽然Windows系统提供了切换快捷健,但对输入工作还是带来了不少麻烦。如果在应用程序中为用户提供智能输入法自动切换,那么这样的应用程序就显得更加专业、更加具有竞争力。不知你可用过Access,在表数据输入时Access自动切换输入法,很酷吧,现在你也可以实现这一切。如果也想你的程式也酷一下的话,请继续...


为了控制输入法,.NET类库在System.Windows.Forms.InputLanguage类中提供了支持。我计划先花一点时间讲述InputLanguage类的功能,随后举一个实例InputLanguageRichEdit。

1、InputLanguage类是一个密封类,它提供了许多方法和属性实现输入法管理功能,这其中有几个属性尤其重要,我将在下面逐一讲解,如果你想全面了解类的全部方法和属性,请浏览MSDN。

public static InputLanguage CurrentInputLanguage {get; set;}
   //获得或设置当前线程的输入法。

public static InputLanguage DefaultInputLanguage {get;}
   //获得缺省输入法。

public static InputLanguageCollection InstalledInputLanguages{get;}
   //获得系统输入法集。可以通过这个容器对象列举系统当前安装的输入法列表。

public string LayoutName {get;}
   //获得输入法在系统托盘中的注册名称。

......

2、我们已经研究了InputLanguage类提供的几个重要属性了,现在可以开始动手在应用开发中应用InputLanguage类。我想创建一个.NET Window Form的系统程序,用一个列表框列举当前系统安装的所有输入法,通过改变列表框的选项自动改变当前线程的输入法。同时还实现了根据桌面托盘中输入法的变化来改变列表框的选项。

(1)、新建项目 --> 选择"Visual C#项目" --> 输入项目名:InputLanguageRichEdit。

(2)、在"工具箱"中拖一个RichTextBox控件,命名为:richTextBox1;一个ComboBox控件,命名为:comboBox1;一个Button控件,命名为:But_Exit。
(3)、用下面的代码代替private void InitializeComponent()。


{
   this.comboBox1 = new System.Windows.Forms.ComboBox();
   this.richTextBox1 = new System.Windows.Forms.RichTextBox();
   this.But_Eixt = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // comboBox1
   //
   this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
   this.comboBox1.DropDownWidth = 160;
   this.comboBox1.Location = new System.Drawing.Point(8, 232);
   this.comboBox1.Name = "comboBox1";
   this.comboBox1.Size = new System.Drawing.Size(168, 20);
   this.comboBox1.TabIndex = 1;
   this.comboBox1.SelectedIndexChanged += new   System.EventHandler(this.comboBox1_SelectedIndexChanged);
   //
   // richTextBox1
   //
   this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
   this.richTextBox1.Name = "richTextBox1";
   this.richTextBox1.Size = new System.Drawing.Size(292, 208);
   this.richTextBox1.TabIndex = 0;
   this.richTextBox1.Text = "";
   //
   // But_Eixt
   //
   this.But_Eixt.Location = new System.Drawing.Point(200, 232);
   this.But_Eixt.Name = "But_Eixt";
   this.But_Eixt.TabIndex = 2;
   this.But_Eixt.Text = "Eixt";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.AddRange(new System.Windows.Forms.Control[] {
   this.But_Eixt,this.comboBox1,this.richTextBox1});
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.InputLanguageChanged += new   System.Windows.Forms.InputLanguageChangedEventHandler(this.ChangeInput);
   this.ResumeLayout(false);
   }

(4)、插入下面代码:

private void Form1_Load(object sender, System.EventArgs e)
   {
    InputLanguageCollection ilc = InputLanguage.InstalledInputLanguages;
    foreach ( InputLanguage il in ilc )
    {
     comboBox1.Items.Add( il.LayoutName );
    }
    comboBox1.SelectedIndex = InputLanguage.InstalledInputLanguages.IndexOf(     InputLanguage.CurrentInputLanguage ) ;
   }

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
   {
    InputLanguage il = InputLanguage.InstalledInputLanguages[ comboBox1.SelectedIndex ];
    InputLanguage.CurrentInputLanguage = il;
   }

private void ChangeInput(object sender, System.Windows.Forms.InputLanguageChangedEventArgs e)
   {
    InputLanguage il = e.InputLanguage ;
    int i = InputLanguage.InstalledInputLanguages.IndexOf( il );
    if( i >= 0 && i < InputLanguage.InstalledInputLanguages.Count )
    {
     comboBox1.SelectedIndex = i ;
    }
   }

private void But_Eixt_Click(object sender, System.EventArgs e)
   {
    Application.Exit();
   }

上一篇:用Visual C#调用Windows API函数    下一篇:用Visual C#来清空回收站  
[发送给好友]  [关闭窗口]  [返回顶部]   转载请注明来源:www.it00.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
责任编辑: 原点 投稿作者: 周毅
信息来源: 网络 录入时间: 2005-8-8 9:55:02
关于我们 - 广告服务 - 版权申明 - 网站地图 - 联系方式 - 总编信箱 - 会员投稿