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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
Photoshop's Key Features: A Deep Dive Photoshop's Key Features: A Deep Dive Apr 19, 2025 am 12:08 AM

Key features of Photoshop include layers and masks, adjustment tools, filters and effects. 1. Layers and masks allow independent editing of image parts. 2. Adjust tools such as brightness/contrast can modify image tone and brightness. 3. Filters and effects can quickly add visual effects. Mastering these features can help creative professionals achieve their creative vision.

Advanced Photoshop Tutorial: Master Retouching & Compositing Advanced Photoshop Tutorial: Master Retouching & Compositing Apr 17, 2025 am 12:10 AM

Photoshop's advanced photo editing and synthesis technologies include: 1. Use layers, masks and adjustment layers for basic operations; 2. Use image pixel values ​​to achieve photo editing effects; 3. Use multiple layers and masks for complex synthesis; 4. Use "liquefaction" tools to adjust facial features; 5. Use "frequency separation" technology to perform delicate photo editing, these technologies can improve image processing level and achieve professional-level effects.

Using Photoshop: Creative Possibilities and Practical Uses Using Photoshop: Creative Possibilities and Practical Uses Apr 22, 2025 am 12:09 AM

Photoshop is very practical and creative in practical applications. 1) It provides basic editing, repairing and synthesis functions, suitable for beginners and professionals. 2) Advanced features such as content recognition fill and layer style can improve image effects. 3) Mastering shortcut keys and optimizing layer structure can improve work efficiency.

Photoshop: Investigating Free Trials and Discount Options Photoshop: Investigating Free Trials and Discount Options Apr 14, 2025 am 12:06 AM

You can get the access to Photoshop in the most economical way: 1. Experience the software features with a 7-day free trial; 2. Find student or teacher discounts, as well as seasonal promotions; 3. Use coupons on third-party websites; 4. Subscribe to Adobe CreativeCloud's monthly or annual plan.

Photoshop and Digital Art: Painting, Illustration, and Compositing Photoshop and Digital Art: Painting, Illustration, and Compositing Apr 18, 2025 am 12:01 AM

Photoshop's applications in digital art include painting, illustration and image synthesis. 1) Painting: Using brushes, pencils and mixing tools, the artist can create realistic effects. 2) Illustration: With vector and shape tools, artists can accurately draw complex graphics and add effects. 3) Synthesis: Using mask and layer blending mode, artists can seamlessly blend different image elements.

Using Photoshop for Graphic Design: Branding and More Using Photoshop for Graphic Design: Branding and More Apr 16, 2025 am 12:02 AM

The steps to using Photoshop for brand design include: 1. Use the Pen tool to draw basic shapes, 2. Add shadows and highlights through layer styles, 3. Adjust colors and details, 4. Use smart objects and actions to automatically generate different versions of the design. Photoshop helps designers create and optimize brand elements with the flexibility of layers and masks, ensuring consistency and professionalism of designs, from simple logos to complex branding guides.

Photoshop for Photographers: Enhancing and Retouching Images Photoshop for Photographers: Enhancing and Retouching Images Apr 25, 2025 am 12:01 AM

Enhance and retouching photos in Photoshop can be achieved by adjusting brightness and contrast, using the Repair Brush Tool. 1) Adjust brightness and contrast: Increase brightness and contrast to improve underexposed photos through the Image->Adjustments->Brightness/Contrast menu. 2) Use the Repair Brush Tool: Select HealingBrushTool in the toolbar and apply to remove miscellaneous points or scars in the image.

What Photoshop Does Best: Common Tasks and Projects What Photoshop Does Best: Common Tasks and Projects Apr 23, 2025 am 12:06 AM

Photoshop is specialized in image editing, layering and masking, digital painting and a variety of design applications. 1) Image editing and repair: remove defects and adjust color and brightness. 2) Layers and masks: non-destructive editing and creation. 3) Digital paintings and illustrations: create art works. 4) Practical applications: graphic design, web design and digital art creation.

See all articles