Home Web Front-end PS Tutorial Implementing Chinese input on a blank form under C# can implement a PS-like text tool

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

Feb 15, 2017 am 10:23 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the common questions about exporting PDF on PS What are the common questions about exporting PDF on PS Apr 06, 2025 pm 04:51 PM

Frequently Asked Questions and Solutions when Exporting PS as PDF: Font Embedding Problems: Check the "Font" option, select "Embed" or convert the font into a curve (path). Color deviation problem: convert the file into CMYK mode and adjust the color; directly exporting it with RGB requires psychological preparation for preview and color deviation. Resolution and file size issues: Choose resolution according to actual conditions, or use the compression option to optimize file size. Special effects issue: Merge (flatten) layers before exporting, or weigh the pros and cons.

What is the reason why PS keeps showing loading? What is the reason why PS keeps showing loading? Apr 06, 2025 pm 06:39 PM

PS "Loading" problems are caused by resource access or processing problems: hard disk reading speed is slow or bad: Use CrystalDiskInfo to check the hard disk health and replace the problematic hard disk. Insufficient memory: Upgrade memory to meet PS's needs for high-resolution images and complex layer processing. Graphics card drivers are outdated or corrupted: Update the drivers to optimize communication between the PS and the graphics card. File paths are too long or file names have special characters: use short paths and avoid special characters. PS's own problem: Reinstall or repair the PS installer.

How to speed up the loading speed of PS? How to speed up the loading speed of PS? Apr 06, 2025 pm 06:27 PM

Solving the problem of slow Photoshop startup requires a multi-pronged approach, including: upgrading hardware (memory, solid-state drive, CPU); uninstalling outdated or incompatible plug-ins; cleaning up system garbage and excessive background programs regularly; closing irrelevant programs with caution; avoiding opening a large number of files during startup.

How to solve the problem of loading when PS is always showing that it is loading? How to solve the problem of loading when PS is always showing that it is loading? Apr 06, 2025 pm 06:30 PM

PS card is "Loading"? Solutions include: checking the computer configuration (memory, hard disk, processor), cleaning hard disk fragmentation, updating the graphics card driver, adjusting PS settings, reinstalling PS, and developing good programming habits.

How to set password protection for export PDF on PS How to set password protection for export PDF on PS Apr 06, 2025 pm 04:45 PM

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

How to use PS Pen Tool How to use PS Pen Tool Apr 06, 2025 pm 10:15 PM

The Pen Tool is a tool that creates precise paths and shapes, and is used by: Select the Pen Tool (P). Sets Path, Fill, Stroke, and Shape options. Click Create anchor point, drag the curve to release the Create anchor point. Press Ctrl/Cmd Alt/Opt to delete the anchor point, drag and move the anchor point, and click Adjust curve. Click the first anchor to close the path to create a shape, and double-click the last anchor to create an open path.

Photoshop for Professionals: Advanced Editing & Workflow Techniques Photoshop for Professionals: Advanced Editing & Workflow Techniques Apr 05, 2025 am 12:15 AM

Photoshop's advanced editing skills include frequency separation and HDR synthesis, and optimized workflows can be automated. 1) Frequency separation technology separates the texture and color details of images. 2) HDR synthesis enhances the dynamic range of images. 3) Automate workflows to improve efficiency and ensure consistency.

Is slow PS loading related to computer configuration? Is slow PS loading related to computer configuration? Apr 06, 2025 pm 06:24 PM

The reason for slow PS loading is the combined impact of hardware (CPU, memory, hard disk, graphics card) and software (system, background program). Solutions include: upgrading hardware (especially replacing solid-state drives), optimizing software (cleaning up system garbage, updating drivers, checking PS settings), and processing PS files. Regular computer maintenance can also help improve PS running speed.

See all articles