SoFunction
Updated on 2025-03-07

Winform implements control input method

This article is not written to show off, but just share it if you find something good. At the same time, you also make a record to facilitate searching in the future.

Start the text

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

ImmGetContext(IntPtr hwnd): Get the input method handle of the window currently being entered

ImmSetOpenStatus(IntPtr himc, bool b): Set the status of the input method

InputLanguage class: provides methods and fields to manage input language; this is the input method management class that comes with winform, and there is detailed information on msdn

2. For example, there are two input methods installed in this system: Sogou Pinyin and Smart ABC (note: There is a space in the middle of Smart ABC, and it is necessary to clearly compare it with underscore).

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

The idea is very simple: first obtain a list of all input methods installed in the operating system, then loop through the list, compare the specified input method name (the name can be set in the configuration file. When installing, it is configured as different input methods according to the needs of different people), and then set the found input method to the current input method. The code is as follows:

      //Set "Sogou Pinyin" as the current input method      foreach (InputLanguage item in )
      {
        if (("Sogou Pinyin"))
        {
           = item;
          break;
        }
      }
      //Set "Smart ABC" as the current input method      foreach (InputLanguage item in )
      {
        if (("Smart ABC"))
        {
           = item;
          break;
        }
      }
      //Set the system default input method to the current input method       = ;

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

The idea is also very simple. You just need to reactivate the input method. Here we use the above API method.

First declare a delegation:

    private delegate void fixImeDele();
    private fixImeDele _fixime;

Then define what the delegate wants to achieve.

      fixime = delegate
      {
        IntPtr HIme = ImmGetContext();
        ImmSetOpenStatus(HIme, true);
      };

Finally, just execute the entrustment.

(fixime);

The final complete code is as follows:

  public partial class Form1 : Form
  {
    [DllImport("")]
    public static extern IntPtr ImmGetContext(IntPtr hwnd);//Get the input method handle of the window currently being entered    [DllImport("")]
    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 txtSogou Pinyin_Enter(object sender, EventArgs e)
    {
      //int index = ();
      //Set "Sogou Pinyin" as the current input method      foreach (InputLanguage item in )
      {
        if (("Sogou Pinyin"))
        {
           = item;
          break;
        }
      }
    }
    private void textBox3_Enter(object sender, EventArgs e)
    {
      //Set "Smart ABC" as the current input method      foreach (InputLanguage item in )
      {
        if (("Smart ABC"))
        {
           = item;
          break;
        }
      }
    }
    private void txtSystem default_Enter(object sender, EventArgs e)
    {
      //Set the system default input method to the current input method       = ;
    }
    private void txtTrue_Enter(object sender, EventArgs e)
    {
      fixime = delegate
      {
        IntPtr HIme = ImmGetContext();
        ImmSetOpenStatus(HIme, true);
      };
      (fixime);
    }
    private void txtFalse_Enter(object sender, EventArgs e)
    {
      fixime = delegate
      {
        IntPtr HIme = ImmGetContext();
        ImmSetOpenStatus(HIme, false);
      };
      (fixime);
    }
  }

I'll share with you a solution to a small problem

The default ImeMode value of wihform is NoControl

This is not suitable for Chinese input.

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

We should set the ImeMode value of the parent window and all child windows to On

But there is a problem with this: it is always full-width state.

The solution is:
Find it in the file in each window

 = ;

Replace with

 = ;

This will be OK.