程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 在C#應用程序中控制輸入法

在C#應用程序中控制輸入法

編輯:關於C#

在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();
}

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