首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 控制應用程式輸入法完整指南(二):完整程式碼與執行示範

     編輯:關於C語言

完整 Form 程式碼


using System;
using System.Windows.Forms;

namespace InputMethodDemo
{
    public class Form1 : Form
    {
        private ComboBox comboBox1 = new ComboBox();
        private RichTextBox richTextBox1 = new RichTextBox();
        private Button btnExit = new Button();

        public Form1()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            // ComboBox 設定
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.DropDownWidth = 160;
            comboBox1.Location = new System.Drawing.Point(8, 232);
            comboBox1.Name = "comboBox1";
            comboBox1.Size = new System.Drawing.Size(168, 20);
            comboBox1.TabIndex = 1;
            comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;

            // RichTextBox 設定
            richTextBox1.Dock = DockStyle.Top;
            richTextBox1.Name = "richTextBox1";
            richTextBox1.Size = new System.Drawing.Size(292, 208);
            richTextBox1.TabIndex = 0;

            // Exit 按鈕
            btnExit.Location = new System.Drawing.Point(200, 232);
            btnExit.Name = "btnExit";
            btnExit.TabIndex = 2;
            btnExit.Text = "Exit";
            btnExit.Click += BtnExit_Click;

            // Form 設定
            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.AddRange(new Control[] { btnExit, comboBox1, richTextBox1 });
            this.Name = "Form1";
            this.Text = "輸入法切換示範";
            this.Load += Form1_Load;
            this.InputLanguageChanged += Form1_InputLanguageChanged;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 1. 列舉系統安裝的所有輸入法
            InputLanguageCollection ilc = InputLanguage.InstalledInputLanguages;
            foreach (InputLanguage il in ilc)
            {
                comboBox1.Items.Add(il.LayoutName);
            }

            // 2. 設定預設選中項為目前輸入法
            comboBox1.SelectedIndex = ilc.IndexOf(InputLanguage.CurrentInputLanguage);
        }

        // 使用者切換下拉選單 -> 變更目前執行緒輸入法
        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            InputLanguage il = InputLanguage.InstalledInputLanguages[comboBox1.SelectedIndex];
            InputLanguage.CurrentInputLanguage = il;
        }

        // 系統輸入法變更 (如熱鍵切換) -> 同步更新下拉選單
        private void Form1_InputLanguageChanged(object sender, InputLanguageChangedEventArgs e)
        {
            InputLanguage il = e.InputLanguage;
            int index = InputLanguage.InstalledInputLanguages.IndexOf(il);
            if (index >= 0 && index < InputLanguage.InstalledInputLanguages.Count)
            {
                comboBox1.SelectedIndex = index;
            }
        }

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

        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
    }
}

關鍵 API 解析

API用途
InputLanguage.InstalledInputLanguages取得系統安裝的所有輸入法集合 (唯讀)
InputLanguage.CurrentInputLanguage取得/設定目前執行緒的輸入法 (靜態屬性)
InputLanguage.DefaultInputLanguage取得系統預設輸入法
InputLanguage.LayoutName取得輸入法在語言列顯示的名稱
Form.InputLanguageChanged 事件輸入法變更時觸發 (含熱鍵/程式切換)

執行畫面示意

輸入法切換示範 1 輸入法切換示範 2

實務應用場景

  • 智慧型輸入法切換:根據控制項類型自動切換 (TextBox 英文、RichTextBox 中文、密碼欄位英文)
  • 多語系表單:不同欄位對應不同語系輸入法
  • 資料錄入優化:減少使用者手動切換輸入法的次數
  • 遠端桌面/虛擬機:解決輸入法狀態不同步問題

進階技巧


// 針對特定控制項設定輸入法 (Enter/Leave 事件)
private void txtEnglish_Enter(object sender, EventArgs e)
{
    InputLanguage.CurrentInputLanguage = 
        InputLanguage.InstalledInputLanguages.Cast()
        .FirstOrDefault(il => il.LayoutName.Contains("English")) 
        ?? InputLanguage.DefaultInputLanguage;
}

private void txtChinese_Enter(object sender, EventArgs e)
{
    InputLanguage.CurrentInputLanguage = 
        InputLanguage.InstalledInputLanguages.Cast()
        .FirstOrDefault(il => il.LayoutName.Contains("Chinese") || il.LayoutName.Contains("微軟")) 
        ?? InputLanguage.CurrentInputLanguage;
}

注意事項

  • CurrentInputLanguage 僅影響**目前執行緒**,不影響其他執行緒或進程
  • WPF 應用請使用 InputLanguageManager 與 InputMethod 類別
  • 部分輸入法 (如第三方) 可能不支援程式切換
  • 管理員權限執行時,可用輸入法清單可能不同
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved