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;
        }
    }
}

 CULTURE   SPEC.CULTURE  ENGLISH NAME
--------------------------------------------------------------
                        Invariant Language (Invariant Country)
af          af-ZA       Afrikaans
af-ZA       af-ZA       Afrikaans (South Africa)
ar          ar-SA       Arabic
ar-AE       ar-AE       Arabic (U.A.E.)
ar-BH       ar-BH       Arabic (Bahrain)
ar-DZ       ar-DZ       Arabic (Algeria)
ar-EG       ar-EG       Arabic (Egypt)
ar-IQ       ar-IQ       Arabic (Iraq)
ar-JO       ar-JO       Arabic (Jordan)
ar-KW       ar-KW       Arabic (Kuwait)
ar-LB       ar-LB       Arabic (Lebanon)
ar-LY       ar-LY       Arabic (Libya)
ar-MA       ar-MA       Arabic (Morocco)
ar-OM       ar-OM       Arabic (Oman)
ar-QA       ar-QA       Arabic (Qatar)
ar-SA       ar-SA       Arabic (Saudi Arabia)
ar-SY       ar-SY       Arabic (Syria)
ar-TN       ar-TN       Arabic (Tunisia)
ar-YE       ar-YE       Arabic (Yemen)
az          az-Latn-AZ  Azeri
az-Cyrl-AZ  az-Cyrl-AZ  Azeri (Cyrillic, Azerbaijan)
az-Latn-AZ  az-Latn-AZ  Azeri (Latin, Azerbaijan)
be          be-BY       Belarusian
be-BY       be-BY       Belarusian (Belarus)
bg          bg-BG       Bulgarian
bg-BG       bg-BG       Bulgarian (Bulgaria)
bs-Latn-BA  bs-Latn-BA  Bosnian (Bosnia and Herzegovina)
ca          ca-ES       Catalan
ca-ES       ca-ES       Catalan (Catalan)
cs          cs-CZ       Czech
cs-CZ       cs-CZ       Czech (Czech Republic)
cy-GB       cy-GB       Welsh (United Kingdom)
da          da-DK       Danish
da-DK       da-DK       Danish (Denmark)
de          de-DE       German
de-AT       de-AT       German (Austria)
de-DE       de-DE       German (Germany)
de-CH       de-CH       German (Switzerland)
de-LI       de-LI       German (Liechtenstein)
de-LU       de-LU       German (Luxembourg)
dv          dv-MV       Divehi
dv-MV       dv-MV       Divehi (Maldives)
el          el-GR       Greek
el-GR       el-GR       Greek (Greece)
en          en-US       English
en-029      en-029      English (Caribbean)
en-AU       en-AU       English (Australia)
en-BZ       en-BZ       English (Belize)
en-CA       en-CA       English (Canada)
en-GB       en-GB       English (United Kingdom)
en-IE       en-IE       English (Ireland)
en-JM       en-JM       English (Jamaica)
en-NZ       en-NZ       English (New Zealand)
en-PH       en-PH       English (Republic of the Philippines)
en-TT       en-TT       English (Trinidad and Tobago)
en-US       en-US       English (United States)
en-ZA       en-ZA       English (South Africa)
en-ZW       en-ZW       English (Zimbabwe)
es          es-ES       Spanish
es-AR       es-AR       Spanish (Argentina)
es-BO       es-BO       Spanish (Bolivia)
es-CL       es-CL       Spanish (Chile)
es-CO       es-CO       Spanish (Colombia)
es-CR       es-CR       Spanish (Costa Rica)
es-DO       es-DO       Spanish (Dominican Republic)
es-EC       es-EC       Spanish (Ecuador)
es-ES       es-ES       Spanish (Spain)
es-GT       es-GT       Spanish (Guatemala)
es-HN       es-HN       Spanish (Honduras)
es-MX       es-MX       Spanish (Mexico)
es-NI       es-NI       Spanish (Nicaragua)
es-PA       es-PA       Spanish (Panama)
es-PE       es-PE       Spanish (Peru)
es-PR       es-PR       Spanish (Puerto Rico)
es-PY       es-PY       Spanish (Paraguay)
es-SV       es-SV       Spanish (El Salvador)
es-UY       es-UY       Spanish (Uruguay)
es-VE       es-VE       Spanish (Venezuela)
et          et-EE       Estonian
et-EE       et-EE       Estonian (Estonia)
eu          eu-ES       Basque
eu-ES       eu-ES       Basque (Basque)
fa          fa-IR       Persian
fa-IR       fa-IR       Persian (Iran)
fi          fi-FI       Finnish
fi-FI       fi-FI       Finnish (Finland)
fo          fo-FO       Faroese
fo-FO       fo-FO       Faroese (Faroe Islands)
fr          fr-FR       French
fr-BE       fr-BE       French (Belgium)
fr-CA       fr-CA       French (Canada)
fr-FR       fr-FR       French (France)
fr-CH       fr-CH       French (Switzerland)
fr-LU       fr-LU       French (Luxembourg)
fr-MC       fr-MC       French (Principality of Monaco)
gl          gl-ES       Galician
gl-ES       gl-ES       Galician (Galician)
gu          gu-IN       Gujarati
gu-IN       gu-IN       Gujarati (India)
he          he-IL       Hebrew
he-IL       he-IL       Hebrew (Israel)
hi          hi-IN       Hindi
hi-IN       hi-IN       Hindi (India)
hr          hr-HR       Croatian
hr-BA       hr-BA       Croatian (Bosnia and Herzegovina)
hr-HR       hr-HR       Croatian (Croatia)
hu          hu-HU       Hungarian
hu-HU       hu-HU       Hungarian (Hungary)
hy          hy-AM       Armenian
hy-AM       hy-AM       Armenian (Armenia)
id          id-ID       Indonesian
id-ID       id-ID       Indonesian (Indonesia)
is          is-IS       Icelandic
is-IS       is-IS       Icelandic (Iceland)
it          it-IT       Italian
it-CH       it-CH       Italian (Switzerland)
it-IT       it-IT       Italian (Italy)
ja          ja-JP       Japanese
ja-JP       ja-JP       Japanese (Japan)
ka          ka-GE       Georgian
ka-GE       ka-GE       Georgian (Georgia)
kk          kk-KZ       Kazakh
kk-KZ       kk-KZ       Kazakh (Kazakhstan)
kn          kn-IN       Kannada
kn-IN       kn-IN       Kannada (India)
ko          ko-KR       Korean
kok         kok-IN      Konkani
kok-IN      kok-IN      Konkani (India)
ko-KR       ko-KR       Korean (Korea)
ky          ky-KG       Kyrgyz
ky-KG       ky-KG       Kyrgyz (Kyrgyzstan)
lt          lt-LT       Lithuanian
lt-LT       lt-LT       Lithuanian (Lithuania)
lv          lv-LV       Latvian
lv-LV       lv-LV       Latvian (Latvia)
mi-NZ       mi-NZ       Maori (New Zealand)
mk          mk-MK       Macedonian
mk-MK       mk-MK       Macedonian (Former Yugoslav Republic of Macedonia)
mn          mn-MN       Mongolian
mn-MN       mn-MN       Mongolian (Cyrillic, Mongolia)
mr          mr-IN       Marathi
mr-IN       mr-IN       Marathi (India)
ms          ms-MY       Malay
ms-BN       ms-BN       Malay (Brunei Darussalam)
ms-MY       ms-MY       Malay (Malaysia)
mt-MT       mt-MT       Maltese (Malta)
nb-NO       nb-NO       Norwegian, Bokmal (Norway)
nl          nl-NL       Dutch
nl-BE       nl-BE       Dutch (Belgium)
nl-NL       nl-NL       Dutch (Netherlands)
nn-NO       nn-NO       Norwegian, Nynorsk (Norway)
no          nb-NO       Norwegian
ns-ZA       ns-ZA       Northern Sotho (South Africa)
pa          pa-IN       Punjabi
pa-IN       pa-IN       Punjabi (India)
pl          pl-PL       Polish
pl-PL       pl-PL       Polish (Poland)
pt          pt-BR       Portuguese
pt-BR       pt-BR       Portuguese (Brazil)
pt-PT       pt-PT       Portuguese (Portugal)
quz-BO      quz-BO      Quechua (Bolivia)
quz-EC      quz-EC      Quechua (Ecuador)
quz-PE      quz-PE      Quechua (Peru)
ro          ro-RO       Romanian
ro-RO       ro-RO       Romanian (Romania)
ru          ru-RU       Russian
ru-RU       ru-RU       Russian (Russia)
sa          sa-IN       Sanskrit
sa-IN       sa-IN       Sanskrit (India)
se-FI       se-FI       Sami (Northern) (Finland)
se-NO       se-NO       Sami (Northern) (Norway)
se-SE       se-SE       Sami (Northern) (Sweden)
sk          sk-SK       Slovak
sk-SK       sk-SK       Slovak (Slovakia)
sl          sl-SI       Slovenian
sl-SI       sl-SI       Slovenian (Slovenia)
sma-NO      sma-NO      Sami (Southern) (Norway)
sma-SE      sma-SE      Sami (Southern) (Sweden)
smj-NO      smj-NO      Sami (Lule) (Norway)
smj-SE      smj-SE      Sami (Lule) (Sweden)
smn-FI      smn-FI      Sami (Inari) (Finland)
sms-FI      sms-FI      Sami (Skolt) (Finland)
sq          sq-AL       Albanian
sq-AL       sq-AL       Albanian (Albania)
sr          sr-Latn-CS  Serbian
sr-Cyrl-BA  sr-Cyrl-BA  Serbian (Cyrillic) (Bosnia and Herzegovina)
sr-Cyrl-CS  sr-Cyrl-CS  Serbian (Cyrillic, Serbia)
sr-Latn-BA  sr-Latn-BA  Serbian (Latin) (Bosnia and Herzegovina)
sr-Latn-CS  sr-Latn-CS  Serbian (Latin, Serbia)
sv          sv-SE       Swedish
sv-FI       sv-FI       Swedish (Finland)
sv-SE       sv-SE       Swedish (Sweden)
sw          sw-KE       Kiswahili
sw-KE       sw-KE       Kiswahili (Kenya)
syr         syr-SY      Syriac
syr-SY      syr-SY      Syriac (Syria)
ta          ta-IN       Tamil
ta-IN       ta-IN       Tamil (India)
te          te-IN       Telugu
te-IN       te-IN       Telugu (India)
th          th-TH       Thai
th-TH       th-TH       Thai (Thailand)
tn-ZA       tn-ZA       Tswana (South Africa)
tr          tr-TR       Turkish
tr-TR       tr-TR       Turkish (Turkey)
tt          tt-RU       Tatar
tt-RU       tt-RU       Tatar (Russia)
uk          uk-UA       Ukrainian
uk-UA       uk-UA       Ukrainian (Ukraine)
ur          ur-PK       Urdu
ur-PK       ur-PK       Urdu (Islamic Republic of Pakistan)
uz          uz-Latn-UZ  Uzbek
uz-Cyrl-UZ  uz-Cyrl-UZ  Uzbek (Cyrillic, Uzbekistan)
uz-Latn-UZ  uz-Latn-UZ  Uzbek (Latin, Uzbekistan)
vi          vi-VN       Vietnamese
vi-VN       vi-VN       Vietnamese (Vietnam)
xh-ZA       xh-ZA       Xhosa (South Africa)
zh-CN       zh-CN       Chinese (People's Republic of China)
zh-HK       zh-HK       Chinese (Hong Kong S.A.R.)
zh-CHS      (none)      Chinese (Simplified)
zh-CHT      (none)      Chinese (Traditional)
zh-MO       zh-MO       Chinese (Macao S.A.R.)
zh-SG       zh-SG       Chinese (Singapore)
zh-TW       zh-TW       Chinese (Taiwan)
zu-ZA       zu-ZA       Zulu (South Africa)

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