小弟写了一个程序用于获取输入法放回的字符串,测试的时候发现谷歌输入法只能接收英文不能接收中文,不知道为什么请高手指教,我测试过了QQ输入法,微软自带的输入法,搜狗输入的都是可以的,代码如下
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 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)
            {
                //m_hImc = ImmGetContext(this.Handle);
                Console.WriteLine("m_hImc:" + m_hImc.ToInt32());
                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_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());
                //        Console.WriteLine(str.ToString());
                //        intoText();
                //        isShowChina = true;
                //    }
                //    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);
        }
    }
}