日期:2014-05-18  浏览次数:20833 次

关于输入汉字文字的问题
最近做到的项目需要用到在贴图区域中输入文字的内容...

以前一直都是用textbox之类的控件输入文字

现在要在rectangle中输入文字,请问要怎么操作才可以激活输入法?

如果只是英文输入的话也就只需要纪录下键盘就得了,可是汉字输入的话...没思路

求思路!如何实现在点击一个rectangle后可以输入文字数据的功能!

------解决方案--------------------
这个我前年弄过,现在大记不清了,大概就是以下的一些API和处理,你可以搜索一下这些API,可能能找到完整示例。
C# code

        //光标API
        [DllImport("user32.dll")]
        static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
        [DllImport("user32.dll")]
        static extern bool ShowCaret(IntPtr hWnd);
        [DllImport("User32.dll")]
        static extern bool HideCaret(IntPtr hWnd);
        [DllImport("User32.dll")]
        static extern bool SetCaretPos(int x, int y);
        [DllImport("user32.dll")]
        static extern bool DestroyCaret();

        //输入法相关API
        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;


        //大概处理过程
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_IME_SETCONTEXT && m.WParam == (IntPtr)1)
            {
                ImmAssociateContext(this.Handle, m_hImc);
            }

            switch (m.Msg)
            {
                case WM_CHAR:
                    char a = (char)m.WParam; //英文 
                       //...............
                    break;
                case WM_IME_CHAR:
                    if (m.WParam.ToInt32() == PM_REMOVE)
                    {
                        int size = ImmGetCompositionString(m_hImc, GCS_COMPSTR, null, 0);
                        size += 2;
                        ImmGetCompositionString(m_hImc, GCS_RESULTSTR, str, size);
                        //..............
                    }
                    break;
            }
        }

------解决方案--------------------
是这个链接http://webservices.ctocio.com.cn/net/92/9142592.shtml