C#控制输入法详细剖析

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

本文向大家介绍C#控制输入法,可能好多人还不知道C#控制输入法,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

在Windows系统一般都安装了至少三种输入法,在输入数据时常常会切换输入法,虽然Windows系统提供了切换快捷健,但对输入工作还是带来了不少麻烦。如果在应用程序中为用户提供智能输入法自动切换,那么这样的应用程序就显得更加专业、更加具有竞争力。不知你可用过Access,在表数据输入时Access自动切换输入法,很酷吧,现在你也可以实现这一切。如果也想你的程式也酷一下的话,请继续...

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

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

  1. public static InputLanguage CurrentInputLanguage {get; set;}  
  2. //获得或设置当前线程的输入法。  
  3.  
  4. public static InputLanguage DefaultInputLanguage {get;}  
  5. //获得缺省输入法。  
  6.  
  7. public static InputLanguageCollection InstalledInputLanguages{get;}  
  8. //获得系统输入法集。可以通过这个容器对象列举系统当前安装的输入法列表。  
  9.  
  10. public string LayoutName {get;}  
  11. //获得输入法在系统托盘中的注册名称。  
  12.  
  13. ......  

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

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

(2)、在"工具箱"中拖一个RichTextBox控件,命名为:richTextBox1;一个ComboBox控件,命名为:comboBox1;一个Button控件,命名为:But_Exit。

(3)、用下面的代码代替

  1. private void InitializeComponent()。  
  2. {  
  3. this.comboBox1 = new System.Windows.Forms.ComboBox();  
  4. this.richTextBox1 = new System.Windows.Forms.RichTextBox();  
  5. this.But_Eixt = new System.Windows.Forms.Button();  
  6. this.SuspendLayout();  
  7. //  
  8. // comboBox1  
  9. //  
  10. this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;  
  11. this.comboBox1.DropDownWidth = 160;  
  12. this.comboBox1.Location = new System.Drawing.Point(8, 232);  
  13. this.comboBox1.Name = "comboBox1";  
  14. this.comboBox1.Size = new System.Drawing.Size(168, 20);  
  15. this.comboBox1.TabIndex = 1;  
  16. this.comboBox1.SelectedIndexChanged += new   System.EventHandler 
    (this.comboBox1_SelectedIndexChanged);  
  17. //  
  18. // richTextBox1  
  19. //  
  20. this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;  
  21. this.richTextBox1.Name = "richTextBox1";  
  22. this.richTextBox1.Size = new System.Drawing.Size(292, 208);  
  23. this.richTextBox1.TabIndex = 0;  
  24. this.richTextBox1.Text = "";  
  25. //  
  26. // But_Eixt  
  27. //  
  28. this.But_Eixt.Location = new System.Drawing.Point(200, 232);  
  29. this.But_Eixt.Name = "But_Eixt";  
  30. this.But_Eixt.TabIndex = 2;  
  31. this.But_Eixt.Text = "Eixt";  
  32. //  
  33. // Form1  
  34. //  
  35. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);  
  36. this.ClientSize = new System.Drawing.Size(292, 273);  
  37. this.Controls.AddRange(new System.Windows.Forms.Control[] {  
  38. this.But_Eixt,this.comboBox1,this.richTextBox1});  
  39. this.Name = "Form1";  
  40. this.Text = "Form1";  
  41. this.Load += new System.EventHandler(this.Form1_Load);  
  42. this.InputLanguageChanged += new   
  43. System.Windows.Forms.InputLanguageChangedEventHandler (this.ChangeInput);  
  44. this.ResumeLayout(false);  

(4)、插入下面代码:

  1. private void Form1_Load(object sender, System.EventArgs e)  
  2. {  
  3. InputLanguageCollection ilc = InputLanguage.InstalledInputLanguages;  
  4. foreach ( InputLanguage il in ilc )  
  5. {  
  6. comboBox1.Items.Add( il.LayoutName );  
  7. }  
  8. comboBox1.SelectedIndex = InputLanguage.InstalledInputLanguages.IndexOf 
    ( InputLanguage.CurrentInputLanguage ) ;  
  9. }  
  10. private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)  
  11. {  
  12. InputLanguage il = InputLanguage.
    InstalledInputLanguages[ comboBox1.SelectedIndex ];  
  13. InputLanguage.CurrentInputLanguage = il;  
  14. }  
  15. private void ChangeInput(object sender, 
    System.Windows.Forms.InputLanguageChangedEventArgs e)  
  16. {  
  17. InputLanguage il = e.InputLanguage ;  
  18. int i = InputLanguage.InstalledInputLanguages.IndexOf( il );  
  19. if( i >= 0 && i < InputLanguage.InstalledInputLanguages.Count )  
  20. {  
  21. comboBox1.SelectedIndex = i ;  
  22. }  
  23. }  
  24. private void But_Eixt_Click(object sender, System.EventArgs e)  
  25. {  
  26. Application.Exit();  
  27. }  

【编辑推荐】

  1. C#窗体里调用浅谈
  2. C#调用ListEmployee命令
  3. C# CreateEmployeeDefinition()函数
  4. C#实现断点续传详细剖析
  5. C# Employee对象浅谈
责任编辑:佚名 来源: 博客园
相关推荐

2009-08-27 17:14:36

C# Socket

2021-08-04 16:36:00

Windows 10Windows微软

2009-09-11 11:17:04

C#引用类型

2009-08-31 17:26:32

C#异常处理

2009-08-27 17:51:34

C#匿名方法

2009-09-03 16:58:49

C#内存管理

2009-09-18 10:00:17

C#数组操作

2021-11-11 23:00:50

Windows 10Windows微软

2009-09-04 11:06:40

C#访问修饰符

2009-09-07 13:42:56

C# Pop3类

2009-08-28 15:38:49

C#实现断点续传

2010-01-08 16:50:51

Ubuntu SCIM

2010-01-11 10:09:14

Ubuntulinux输入法设置

2010-06-08 18:50:30

OpenSUSE 输入

2012-02-15 11:14:21

搜狗输入法

2021-10-15 22:17:08

Windows 10Windows微软

2021-04-20 08:30:23

微信微信输入法张小龙

2011-09-15 09:34:48

ubuntu输入法

2013-01-10 11:27:58

Android输入法分析报告

2010-03-25 13:19:17

云计算
点赞
收藏

51CTO技术栈公众号