WinForm How to control Input method

Source: Internet
Author: User

Original: WinForm How to control Input method

In the work encountered such a problem, in the system use process, the input method will be grayed out to use input methods can not enter text, as if the input method is disabled is not enabled. Here's a memo on this.

Here the article is not written to show off what, just think found some good things to share it, but also make a record, convenient to find later

Start text

1, first introduced the article will be used by the Windows API, online has very detailed information, I here just briefly explain

Immgetcontext (INTPTR hwnd): Gets the IME handle of the window currently being entered

Immsetopenstatus (IntPtr HIMC, bool B): Sets the state of the input method

InputLanguage class: provides methods and fields to manage the input language; This is the WinForm that comes with the input Method management class, MSDN has detailed information

2, such as the installation of the system has two input methods: sogou Pinyin and intelligent ABC(NOTE: Smart ABC has a space in the middle, with the underscore is to be clear comparison).

3. Use the InputLanguage class to display the specified input method.

The idea is simple: first get to the operating system installed in the list of all input methods, and then loop the list, than the specified input method name (the name can be set in the configuration file, installation, according to the needs of different people configured to different input methods), and then the found input method is set as the current input method, the code is as follows:

  

            //set "Sogou Pinyin" as the current input method            foreach(InputLanguage Iteminchinputlanguage.installedinputlanguages) {if(item. Layoutname.contains ("sogou Pinyin") ) {Inputlanguage.currentinputlanguage=item;  Break; }            }            //Set "Smart ABC" as the current input method            foreach(InputLanguage Iteminchinputlanguage.installedinputlanguages) {if(item. Layoutname.contains ("Smart ABC") ) {Inputlanguage.currentinputlanguage=item;  Break; }            }            //sets the system default input method to the current input methodInputlanguage.currentinputlanguage = Inputlanguage.defaultinputlanguage;

3. Solve the illusion that input method is disabled.

The idea is also very simple, only need to reactivate the input method on the line, here is used in the above API method.

First declare a delegate:

  

        Private Delegate void Fiximedele ();         private Fiximedele _fixime;

Then define what the delegate wants to accomplish.

            Delegate             {                = Immgetcontext (this. Handle);                 true );            };

Finally, the execution of the delegation is OK.

this. BeginInvoke (Fixime);

The final complete code is as follows:

     Public Partial classForm1:form {[DllImport ("Imm32.dll")]         Public Static externINTPTR Immgetcontext (INTPTR hwnd);//gets the IME handle for the window currently being entered[DllImport ("Imm32.dll")]         Public Static extern BOOLImmsetopenstatus (IntPtr hIMC,BOOLb);//set the state of an input method        Private Delegate voidFiximedele (); PrivateFiximedele Fixime;  PublicForm1 () {InitializeComponent (); }                Private voidtxt sogou Pinyin _enter (Objectsender, EventArgs e) {            //int index = INPUTLANGUAGE.INSTALLEDINPUTLANGUAGES.INDEXOF (inputlanguage.currentinputlanguage); //set "Sogou Pinyin" as the current input method            foreach(InputLanguage Iteminchinputlanguage.installedinputlanguages) {if(item. Layoutname.contains ("sogou Pinyin") ) {Inputlanguage.currentinputlanguage=item;  Break; }            }        }                Private voidTextbox3_enter (Objectsender, EventArgs e) {            //Set "Smart ABC" as the current input method            foreach(InputLanguage Iteminchinputlanguage.installedinputlanguages) {if(item. Layoutname.contains ("Smart ABC") ) {Inputlanguage.currentinputlanguage=item;  Break; }            }        }        Private voidTXT system default _enter (Objectsender, EventArgs e) {            //sets the system default input method to the current input methodInputlanguage.currentinputlanguage =Inputlanguage.defaultinputlanguage; }        Private voidTxttrue_enter (Objectsender, EventArgs e) {Fixime=Delegate{IntPtr Hime= Immgetcontext ( This.                Handle); Immsetopenstatus (Hime,true);            };  This.        BeginInvoke (Fixime); }        Private voidTxtfalse_enter (Objectsender, EventArgs e) {Fixime=Delegate{IntPtr Hime= Immgetcontext ( This.                Handle); Immsetopenstatus (Hime,false);            };  This.        BeginInvoke (Fixime); }    }
View Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.