C# 輸入法

/*
 參考來源:
 http://reflector.webtropy.com/default.aspx/4@0/4@0/untmp/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/fx/src/WinForms/Managed/System/WinForms/InputLanguage@cs/1305376/InputLanguage@cs
http://msdn.microsoft.com/en-us/library/ms171548.aspx
http://msdn.microsoft.com/en-us/library/syy068tk.aspx
http://go4answers.webhost4life.com/Example/changing-keyboard-layout-issue-218141.aspx
http://www.csharp-examples.net/culture-names/
http://www.codeproject.com/Articles/8980/Instantly-Changing-Language-in-the-Form
http://www.koders.com/csharp/fid96D51DDF229ADAF1470D43CFE9C6277B051AC885.aspx?s=file#L2
http://msdn.microsoft.com/en-us/library/system.windows.forms.inputlanguage%28v=vs.71%29.aspx
http://blogs.msdn.com/b/snippets/archive/2008/12/31/how-to-change-input-language-programmatically.aspx
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/c7a82a6a-763e-424b-84e0-496caa9cfb4d/
 */

using System;
using System.Collections.Generic;
using System.Collections; 
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Globalization;

namespace InputLanguageDemo
{
    /// <summary>
    /// 20120521 Geovin Du
    /// Csharp 輪入法操作 來源網絡
    /// 塗聚文
    /// </summary>
    public partial class Form1 : Form
    {
        #region 
        //[DllImport("user32")]
        //private static extern uint ActivateKeyboardLayout(uint hkl, uint Flags);
        //[DllImport("user32")]
        //private static extern uint LoadKeyboardLayout(string pwszKLID, uint Flags);
        //[DllImport("user32")]
        //private static extern uint GetKeyboardLayoutList(int nBuff, uint[] List);
        //private static Hashtable FImes;
        //public static uint KLF_ACTIVATE = 1; 
        #endregion 

        /// <summary>
        /// 
        /// </summary>
        public Form1()
        {
            InitializeComponent();
            //半角,全角問題
            if (this.ImeMode != ImeMode.Hangul)
                this.ImeMode = ImeMode.Hangul;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {

            ComboboxItem item = new ComboboxItem();
            InputLanguageCollection ilc = InputLanguage.InstalledInputLanguages;
            foreach (InputLanguage il in ilc)
            {
                comboBox1.Items.Add(il.LayoutName);
            }
            comboBox1.SelectedIndex = InputLanguage.InstalledInputLanguages.IndexOf(InputLanguage.CurrentInputLanguage);


            //CULTURE   SPEC.CULTURE  ENGLISH NAME
            // get culture names
            List<string> list = new List<string>();
            foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
            {
                string specName = "(none)";
                try { specName = CultureInfo.CreateSpecificCulture(ci.Name).Name; }
                catch { }
                list.Add(String.Format("{0,-12}{1,-12}{2}", ci.Name, specName, ci.EnglishName));
                
                //1
                //comboBox2.Items.Add(new Item(ci.Name + "/" + specName + "/" + ci.EnglishName, ci.EnglishName));
                //comboBox2.SelectedIndex = 0;
                //item.Text = "22";
                //item.Value = "2";
                //comboBox2.Items.Add(item);
            }

            list.Sort();  // sort by name
            //2
            comboBox2.DataSource = list;

            string[] languagename = new string[] { "王码五笔型86版", "五笔型86版" };//存在选择最后的关键字
            for (int i = 0; i < languagename.Length; i++)
            {
                //设置当前输入法
                foreach (InputLanguage iL in InputLanguage.InstalledInputLanguages)
                {
                    //1
                    //if (iL.LayoutName == "中文 (简体) - 王码五笔型86版")
                    //{
                    //    InputLanguage.CurrentInputLanguage = iL;
                    //    break;
                    //}
                    //2
                    if (iL.LayoutName.Contains(languagename[i]))
                    {
                        InputLanguage.CurrentInputLanguage = iL;////获取当前输入法信息
                        break;
                    }

                }
            }
            //获取当前输入法信息
            InputLanguage CurrentInput = InputLanguage.CurrentInputLanguage;
            this.textBox1.Text = CurrentInput.LayoutName;
            //获取输入法的语言区域
            this.textBox3.Text = CurrentInput.Culture.DisplayName;
            //获取默认的输入法信息
            InputLanguage DefaultInput = InputLanguage.DefaultInputLanguage;
            this.textBox2.Text = DefaultInput.LayoutName;


        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            InputLanguage il = InputLanguage.InstalledInputLanguages[comboBox1.SelectedIndex];
            InputLanguage.CurrentInputLanguage = il; 
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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;
            }
        }

        #region 
        ////设定当前Ime,使用方法Ime.SetImeName("中文 (简体) - 拼音");
        //public static void SetImeName(string ImeName)
        //{
        //    //字符串形式
        //    if (FImes == null)
        //        GetImes();
        //    uint id = Convert.ToUInt32(FImes[ImeName]);
        //    SetIme(id);
        //}

        //public static void SetIme(uint ImeId)
        //{
        //    //Id样式
        //    if (ImeId > 0)
        //        ActivateKeyboardLayout(ImeId, KLF_ACTIVATE);
        //}

        ////获得所有的Ime列表
        //public static Hashtable GetImes()
        //{
        //    if (FImes == null)
        //        FImes = new Hashtable();
        //    else
        //        return FImes;
        //    uint[] KbList = new uint[64];
        //    uint TotalKbLayout = GetKeyboardLayoutList(64, KbList);

        //    for (int i = 0; i < TotalKbLayout; i++)
        //    {
        //        string RegKey = String.Format("System//CurrentControlSet//Control//Keyboard Layouts//{0:X8}", KbList[i]);
        //        RegistryKey rk = Registry.LocalMachine.OpenSubKey(RegKey);
        //        if (rk == null)
        //            continue;
        //        string ImeName = (string)rk.GetValue("layout text");
        //        if (ImeName == null)
        //            continue;
        //        FImes.Add(ImeName, KbList[i]);
        //    }
        //    return FImes;
        //}
        #endregion 

        #region 
        /// <summary>
        /// Changing Current Input Language to a next installed language
        /// </summary>
        public void ChangeInputLanguage()
        {

            // Nothing to do if there is only one Input Language supported:
            if (InputLanguage.InstalledInputLanguages.Count == 1)
                return;
            // Get index of current Input Language
            int currentLang = InputLanguage.InstalledInputLanguages.IndexOf(InputLanguage.CurrentInputLanguage);
            // Calculate next Input Language
            InputLanguage nextLang = ++currentLang == InputLanguage.InstalledInputLanguages.Count ?
               InputLanguage.InstalledInputLanguages[0] : InputLanguage.InstalledInputLanguages[currentLang];

            // Change current Language to the calculated:
            ChangeInputLanguage(nextLang);

        }

        /// <summary>
        /// Changing current Input Language to a new one passed in the param
        /// </summary>
        /// <param name="ISOLang">ISO Culture name string code e.g. "en" for English</param>
        public void ChangeInputLanguage(string ISOLang)
        {

            // Convert ISO Culture name to InputLanguage object. Be aware: if ISO is not supported
            // ArgumentException will be invoked here
            InputLanguage nextLang = InputLanguage.FromCulture(new System.Globalization.CultureInfo(ISOLang));
            ChangeInputLanguage(nextLang);

        }

        /// <summary>
        /// Changing current Input Language to a new one passed in the param
        /// </summary>
        /// <param name="LangID">Integer Culture code e.g. 1033 for English</param>
        public void ChangeInputLanguage(int LangID)
        {

            // Convert Integer Culture code to InputLanguage object. Be aware: if Culture code is not supported
            // ArgumentException will be invoked here
            InputLanguage nextLang = InputLanguage.FromCulture(new System.Globalization.CultureInfo(LangID));
            ChangeInputLanguage(nextLang);

        }

        /// <summary>
        /// Changing current Input Language to a new one passed in the param
        /// </summary>
        /// <param name="InputLang">New Input Language as InputLanguage object</param>
        public void ChangeInputLanguage(InputLanguage InputLang)
        {

            // Check is this Language really installed. Raise exception to warn if it is not:
            if (InputLanguage.InstalledInputLanguages.IndexOf(InputLang) == -1)
                throw new ArgumentOutOfRangeException();
            // InputLAnguage changes here:
            InputLanguage.CurrentInputLanguage = InputLang;

        }
        #endregion 

        /// <summary>
        /// 设置当前输入框的输入法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_Enter(object sender, EventArgs e)
        {
            string[] languagename = new string[] { "拼音", "中文", "五笔" };
            for (int i = 0; i < languagename.Length; i++)
            {
                foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
                {
                    if (lang.LayoutName.Contains(languagename[i]))
                    {
                        InputLanguage.CurrentInputLanguage = lang;////获取当前输入法信息
                        this.textBox1.Text = lang.LayoutName ;
                        break;
                    }
                }
            }
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class Item
    {
        public string Name;
        public string Value;
        public Item(string name, string value)
        {
            Name = name; Value = value;
        }
        public override string ToString()
        {
            // Generates the text shown in the combo box
            return Name;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class ComboboxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }

        public override string ToString()
        {
            return Text;
        }
    }
}


你可能感兴趣的:(C#)