Home > Web Front-end > PS Tutorial > body text

Implementing Chinese input on a blank form under C# can implement a PS-like text tool

高洛峰
Release: 2017-02-15 10:23:44
Original
1684 people have browsed it

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.


The code is as follows:


f (m.Msg == WM_IME_SETCONTEXT && m.WParam.ToInt32() == 1)


{


##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.


When the input method is turned on to input text, the WM_IME_CHAR message will be sent. We can get the Chinese characters by processing this message


You can use the IMM function: ImmGetCompositionString(IntPtr hIMC, int dwIndex, StringBuilder lpBuf, int dwBufLen); to obtain the Chinese characters or NUICODE type characters entered on the typing form. Of course, we will not study them here. Nothing else. Let’s just talk about Chinese characters.


The code to extract characters is as follows:


case WM_IME_CHAR:

# int size = ImmGetCompositionString(m_hImc, GCS_COMPSTR, null, 0);

##                                                                                  size += sizeof(Char); m_hImc, GCS_RESULTSTR, str, size);

## sb.Append(str.ToString());


MessageBox.Show(str.ToString());


# intoText();// Print text


##       isShowChina = true;



##                                                                                                   . It seems to be done. After testing it, I found that the printed words are all repeated words. For example, if I enter "Serve the People", what prints out is "Serve the People, Serve the People." Oh my god, there is a problem. Where is it.


## 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.

After reading an online discussion, I came up with a solution: add Judgment


if (m.WParam.ToInt32() == PM_REMOVE)


{


}


Test. Finally there is no problem



Code post


using System;

using System.Collections.Generic;


using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Text;

using System.Windows.Forms;

using System.Runtime .InteropServices;

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!