Home > Article > Web Front-end > Implementing Chinese input on a blank form under C# can implement a PS-like text tool
Realizing Chinese input on a blank form under C
#Keywords: PS-like text tool, Chinese input. Repeated interception of Chinese
##Recently I have been researching on making a PS-like text tool. I checked a lot of information and asked a lot of people. Finally, the hard work paid off. I finally got it. Write it out for everyone to discuss. Open the input method on a blank form. In C#, the blank window is the same no matter what. The input method cannot be opened. Even if this.ImeMode= ImeMode.NoControl is set, the input method recording window cannot be opened. I went to the Microsoft Development Forum and asked some questions. Thanks to moderator Zhou Xuefeng and Riquel_Dong for their advice. I used the API. Function: ImmAssociateContext(IntPtr hWnd, IntPtr hIMC); Finally the input method is called up. Its function is to associate the input with the specified window.##ImmAssociateContext(this.Handle, m_hImc);
}
##Now the input method can be called. But a new problem has arisen. How to get the words on the input method recording form.
# int size = ImmGetCompositionString(m_hImc, GCS_COMPSTR, null, 0);
## size += sizeof(Char); m_hImc, GCS_RESULTSTR, str, size);
# intoText();// Print text
## I checked MSDN. There is this description of WM_IME_CHAR:
the WM_IME_CHAR message includes a double-byte character and the application passes this message to DefWindowProc
Is this a problem? That's where it comes in. It's a problem of sending messages twice.
Code post
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
namespace WindowsApplication2
{
public partial class UserControl1 : UserControl
{
IntPtr m_hImc;
bool isShowChina = false;
public const int WM_IME_SETCONTEXT = 0x0281;
private const int WM_IME_CHAR = 0x0286;
private const int WM_CHAR = 0x0102;
private const int WM_IME_COMPOSITION = 0x010F;
private const int GCS_COMPSTR = 0x0008;
[DllImport("Imm32.dll")]
public static extern IntPtr ImmGetContext(IntPtr hWnd);
[DllImport("Imm32.dll")]
public static extern IntPtr ImmAssociateContext(IntPtr hWnd, IntPtr hIMC);
[DllImport("imm32.dll")]
static extern int ImmGetCompositionString(IntPtr hIMC, int dwIndex, StringBuilder lpBuf, int dwBufLen);
private int GCS_RESULTSTR = 0x0800;
private const int HC_ACTION = 0;
private const int PM_REMOVE = 0x0001;
StringBuilder sb = new StringBuilder();
Font font = new Font("宋体", 14, FontStyle.Regular);
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
m_hImc = ImmGetContext(this.Handle);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_IME_SETCONTEXT && m.WParam.ToInt32() == 1)
{
ImmAssociateContext(this.Handle, m_hImc);
}
switch (m.Msg)
{
case WM_CHAR:
KeyEventArgs e = new KeyEventArgs(((Keys)((int)((long)m.WParam))) | ModifierKeys);
char a = (char)e.KeyData; //英文
sb.Append(a);
intoText();
isShowChina = false;
break;
case WM_IME_CHAR:
if (m.WParam.ToInt32() == PM_REMOVE) //如果不做这个判断.会打印出重复的中文
{
StringBuilder str = new StringBuilder();
int size = ImmGetCompositionString(m_hImc, GCS_COMPSTR, null, 0);
size += sizeof(Char);
ImmGetCompositionString(m_hImc, GCS_RESULTSTR, str, size);
sb.Append(str.ToString());
MessageBox.Show(str.ToString());
intoText();
isShowChina = true;
### Graphics g = this.CreateGraphics();
Graphics g = this.CreateGraphics();
g.DrawString(sb.ToString(), font, Brushes.Black, 10, 10);
}
}}Even To realize Chinese input on a blank form under multiple C#, you can realize a PS-like text tool. For related articles, please pay attention to the PHP Chinese website!