C#控制輸入法詳細(xì)剖析
本文向大家介紹C#控制輸入法,可能好多人還不知道C#控制輸入法,沒(méi)有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。
在Windows系統(tǒng)一般都安裝了至少三種輸入法,在輸入數(shù)據(jù)時(shí)常常會(huì)切換輸入法,雖然Windows系統(tǒng)提供了切換快捷健,但對(duì)輸入工作還是帶來(lái)了不少麻煩。如果在應(yīng)用程序中為用戶提供智能輸入法自動(dòng)切換,那么這樣的應(yīng)用程序就顯得更加專業(yè)、更加具有競(jìng)爭(zhēng)力。不知你可用過(guò)Access,在表數(shù)據(jù)輸入時(shí)Access自動(dòng)切換輸入法,很酷吧,現(xiàn)在你也可以實(shí)現(xiàn)這一切。如果也想你的程式也酷一下的話,請(qǐng)繼續(xù)...
為了C#控制輸入法,.NET類庫(kù)在System.Windows.Forms.InputLanguage類中提供了支持。我計(jì)劃先花一點(diǎn)時(shí)間講述InputLanguage類的功能,隨后舉一個(gè)實(shí)例InputLanguageRichEdit。
1、InputLanguage類是一個(gè)密封類,它提供了許多方法和屬性實(shí)現(xiàn)輸入法管理功能,這其中有幾個(gè)屬性尤其重要,我將在下面逐一講解,如果你想全面了解類的全部方法和屬性,請(qǐng)瀏覽MSDN。
- public static InputLanguage CurrentInputLanguage {get; set;}
- //獲得或設(shè)置當(dāng)前線程的輸入法。
- public static InputLanguage DefaultInputLanguage {get;}
- //獲得缺省輸入法。
- public static InputLanguageCollection InstalledInputLanguages{get;}
- //獲得系統(tǒng)輸入法集??梢酝ㄟ^(guò)這個(gè)容器對(duì)象列舉系統(tǒng)當(dāng)前安裝的輸入法列表。
- public string LayoutName {get;}
- //獲得輸入法在系統(tǒng)托盤中的注冊(cè)名稱。
- ......
2、我們已經(jīng)研究了InputLanguage類提供的幾個(gè)重要屬性了,現(xiàn)在可以開始動(dòng)手在應(yīng)用開發(fā)中應(yīng)用InputLanguage類。我想創(chuàng)建一個(gè).NET Window Form的系統(tǒng)程序,用一個(gè)列表框列舉當(dāng)前系統(tǒng)安裝的所有輸入法,通過(guò)改變列表框的選項(xiàng)自動(dòng)改變當(dāng)前線程的輸入法。同時(shí)還實(shí)現(xiàn)了根據(jù)桌面托盤中C#控制輸入法的變化來(lái)改變列表框的選項(xiàng)。
(1)、新建項(xiàng)目 --> 選擇"Visual C#項(xiàng)目" --> 輸入項(xiàng)目名:InputLanguageRichEdit。
(2)、在"工具箱"中拖一個(gè)RichTextBox控件,命名為:richTextBox1;一個(gè)ComboBox控件,命名為:comboBox1;一個(gè)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();
- }
【編輯推薦】