WEB开发网
开发学院软件开发C语言 C#下实现空白窗体上中文输入,可以实现类PS的文字工... 阅读

C#下实现空白窗体上中文输入,可以实现类PS的文字工具

 2010-09-30 20:56:50 来源:WEB开发网   
核心提示: OK,.好象是大功告成了.测试了一下才发现打印出来的都是重复的文字.比如输入”为人民服务”,打印出的却是”为为人人民民服服务务”我的天呐,问题出在哪里呢.去查了一下MSDN.对WM_IME_CHAR有这样的说明:theWM_IME_CHARmess

OK,.好象是大功告成了.测试了一下才发现打印出来的都是重复的文字.比如输入”为人民服务”,打印出的却是”为为人人民民服服务务”我的天呐,问题出在哪里呢.

去查了一下MSDN.对WM_IME_CHAR有这样的说明:

the WM_IME_CHAR message includes a double-byte character and the application passes this message to DefWindowProc

是不是问题就出在这里了.是发送消息两次的问题.

看了一个网上的讨论,得出一个解决方案:加上判断

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

测试.终于没有了问题了

代码帖子上

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;
                    }
                   
                    break;
            }
      
        }
        /// <summary>
        /// 打印文字
        /// </summary>
        private void intoText()//
        {
           Graphics g  = this.CreateGraphics();
           g.DrawString(sb.ToString(), font, Brushes.Black, 10, 10);
         
        }

    }
}

C#下实现空白窗体上中文输入,可以实现类PS的文字工具

上一页  1 2 

Tags:实现 空白 窗体

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接