Home>

I encountered such a problem at work,During system use,Input methods are grayed out and text cannot be entered using the input method.It's as if the input method is disabled and not enabled.For this, make a note here

Start text

1. First introduce the Windows API used in this article. There are very detailed information on the Internet.I will just briefly explain here

Immgetcontext (intptr hwnd):Get the input method handle of the window currently being input

Immsetopenstatus (intptr himc, bool b):set the status of the input method

Inputlanguage class:provides methods and fields to manage the input language;This is the input method management class that comes with winform.detailed information on msdn

2. For example, this system has two input methods:Sogou Pinyin and Intelligent abc (Note:There is a space in the middle of Intelligent abc.Use underscores to make a clear comparison).

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

The idea is simple:first get a list of all input methods installed in the operating system,Then loop through the list,Match the specified input method name (the name can be set in the configuration file,When installing,Configure different input methods according to the needs of different people), and then set the found input method as the current input method,code show as below:

     //Set "Sogou Pinyin" as the current input method
      foreach (inputlanguage item in inputlanguage.installedinputlanguages)
      {
        if (item.layoutname.contains ("Sogou Pinyin"))
        {
          inputlanguage.currentinputlanguage=item;
          break;
        }
      }
      //Set "smart abc" as the current input method
      foreach (inputlanguage item in inputlanguage.installedinputlanguages)
      {
        if (item.layoutname.contains ("smart abc"))
        {
          inputlanguage.currentinputlanguage=item;
          break;
        }
      }
      //Set the system default input method to the current input method
      inputlanguage.currentinputlanguage=inputlanguage.defaultinputlanguage;

3. Solve the illusion that the input method is disabled.

The idea is also very simple,Just need to reactivate the input method,The above api method is used here.

First declare a delegate:

   private delegate void fiximedele ();
    private fiximedele _fixime;

Then define what the delegate wants to achieve.

     fixime=delegate
      {
        intptr hime=immgetcontext (this.handle);
        immsetopenstatus (hime, true);
      };

Finally, we just need to execute the commission.

this.begininvoke (fixime);

The final complete code is as follows:

 public partial class form1:form
  {
    [dllimport ("imm32.dll")]
    public static extern intptr immgetcontext (intptr hwnd);//Get the input method handle of the window currently being input
    [dllimport ("imm32.dll")]
    public static extern bool immsetopenstatus (intptr himc, bool b);//Set the status of the input method
    private delegate void fiximedele ();
    private fiximedele fixime;
    public form1 ()
    {
      initializecomponent ();
    }
    private void txt Sogou Pinyin_enter (object sender, eventargs e)
    {
      //int index=inputlanguage.installedinputlanguages.indexof (inputlanguage.currentinputlanguage);
      //Set "Sogou Pinyin" as the current input method
      foreach (inputlanguage item in inputlanguage.installedinputlanguages)
      {
        if (item.layoutname.contains ("Sogou Pinyin"))
        {
          inputlanguage.currentinputlanguage=item;
          break;
        }
      }
    }
    private void textbox3_enter (object sender, eventargs e)
    {
      //Set "smart abc" as the current input method
      foreach (inputlanguage item in inputlanguage.installedinputlanguages)
      {
        if (item.layoutname.contains ("smart abc"))
        {
          inputlanguage.currentinputlanguage=item;
          break;
        }
      }
    }
    private void txt system default_enter (object sender, eventargs e)
    {
      //Set the system default input method to the current input method
      inputlanguage.currentinputlanguage=inputlanguage.defaultinputlanguage;
    }
    private void txttrue_enter (object sender, eventargs e)
    {
      fixime=delegate
      {
        intptr hime=immgetcontext (this.handle);
        immsetopenstatus (hime, true);
      };
      this.begininvoke (fixime);
    }
    private void txtfalse_enter (object sender, eventargs e)
    {
      fixime=delegate
      {
        intptr hime=immgetcontext (this.handle);
        immsetopenstatus (hime, false);
      };
      this.begininvoke (fixime);
    }
  }

Let me share a solution to a small problem

wihform default imemode value is nocontrol

This is not suitable for Chinese input.

Because for example:you have to switch the input method every time you get to a control.

We should set the imemode value of the parent window and all child windows to on

The problem with this is that it is always full angle.

The solution is:

Found in desinger.cs file of each window break

this.imemode=system.windows.forms.imemode.on;

Replaced with

this.imemode=system.windows.forms.imemode.onhalf;

That's OK.

c
  • Previous C # yield wrong usage in WCF (1)
  • Next Detailed explanation of several small questions about SQL (sql advanced)