none
.NET 2.0 接收private const int WM_CHAR = 0x0102;时候google输入法无法接收到中文只能接收到英文字符串,而其他输入法(如QQ输入法,搜狗输入法,微软自带的输入法)都可以接收中英文,请教是什么问题呢?代码如下 RRS feed

  • 问题

  • 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 WindowsFormsApplication4
    {
        public partial class UserControl1 : UserControl
        {
            IntPtr m_hImc;
            bool isShowChina = false;
            /// <summary>
            /// Sent to an application when a window is activated. A window receives this message through its WindowProc function.
            /// 窗体activated的时候传系统发这个消息
            /// </summary>
            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;//Sent to an application when the IME changes composition status as a result of a keystroke. A window receives this message through its WindowProc function
            private const int WM_IME_ENDCOMPOSITION = 270;
            private const int WM_IME_STARTCOMPOSITION = 0x10d;
            private const int GCS_COMPSTR = 0x0008;//Retrieves the string resulting from conversion.

            /// <summary>
            /// Returns the input context associated with the specified window.获取与窗口关联的输入法的句柄
            /// The application must call ImmReleaseContext when it is finished with the input context.用完后要释放
            /// </summary>
            /// <param name="hWnd"></param>
            /// <returns></returns>
            [DllImport("Imm32.dll")]
            public static extern IntPtr ImmGetContext(IntPtr hWnd);
            /// <summary>
            /// Associates the specified input context with the specified window. By default, the operating system associates the default input context with each window as it is created.
            /// 将窗口和输入法关联起来
            /// When associating an input context with a window, an application must remove the association before destroying the input context. One way to do this is to save the handle and reassociate it to the default input context with the window.
            /// 关联好输入法不用以后要把它移除,让窗体用系统默认的输入法
            /// </summary>
            /// <param name="hWnd"></param>
            /// <param name="hIMC"></param>
            /// <returns></returns>
            [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;
                        Console.WriteLine(a);
                        sb.Append(a);
                        intoText();
                        isShowChina = false;
                        break;
                    //case WM_IME_STARTCOMPOSITION:
                    //    break;
                    //case WM_IME_COMPOSITION:
                    //    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());
                    //    Console.WriteLine(str);
                    //    //MessageBox.Show(str.ToString());
                    //    //Console.WriteLine(str.ToString());
                       
                    //    isShowChina = true;
                    //    break;
                    //case WM_IME_ENDCOMPOSITION:
                    //    intoText();
                    //    sb = new StringBuilder();
                    //    break;
                  
                }
            }
            /// 
            /// 打印文字
            /// 
            private void intoText()//
            {

                Graphics g = this.CreateGraphics();
                g.DrawString(sb.ToString(), font, Brushes.Black, 10, 10);
            }

            /// 
            /// 打印文字
            /// 
            private void intoText(char a)//
            {

                Graphics g = this.CreateGraphics();
                g.DrawString(a.ToString(), font, Brushes.Black, 10, 10);
            }
        }
    }

    • 已移动 Mike Feng 2012年7月23日 2:01 输入法 (发件人:.NET Framework 一般性问题讨论区)
    2012年7月18日 7:47

全部回复