c# 获取输入法结果

Bob66666 2014-02-27 03:10:08
if (m.Msg == WM_IME_CHAR)
{
Debug.WriteLine("W:"+m.WParam.ToInt32());
StringBuilder str = new StringBuilder();
int size = ImmGetCompositionString(imeHandle, GCS_RESULTSTR, null, 0);
Debug.WriteLine("size:" + size);
size += sizeof(Char);
ImmGetCompositionString(imeHandle, GCS_RESULTSTR, str, size);
Debug.WriteLine(str.ToString().Substring(0, str.Length));
}


测试结果: 输入单个汉字,结果正常,输入多个汉字,结果会出现多次重复,
例如: 输入 我 ,结果是:我
输入 我们,结果是:我们 我们
输入 中国人,结果是:中国人 中国人 中国人

不知此问题何解?
...全文
588 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
feiyun0112 2014-02-27
  • 打赏
  • 举报
回复
把后面多余的部分去掉啊 byte[] bytes = Encoding.Default.GetBytes(str.ToString()); Debug.WriteLine(Encoding.Default.GetString(bytes, 0, size));
Bob66666 2014-02-27
  • 打赏
  • 举报
回复
引用 4 楼 feiyun0112 的回复:
试试WM_IME_COMPOSITION


修改后的代码如下,但每次后面会多出一些乱码。
if (m.Msg == WM_IME_COMPOSITION) {
if ((m.LParam.ToInt32() & GCS_RESULTSTR )!= 0) {
Debug.WriteLine("W:" + m.WParam.ToInt32() + " L:" + m.LParam.ToInt32());
StringBuilder str = new StringBuilder();
//int size = ImmGetCompositionString(imeHandle, GCS_RESULTSTR, null, 0);
//Debug.WriteLine("size:" + size);
//size += sizeof(Char);
ImmGetCompositionString(imeHandle,GCS_RESULTSTR, str, 1024);
Debug.WriteLine(str.ToString().Substring(0,str.Length));
}

}

汉字后面会多出几个字符.
Bob66666 2014-02-27
  • 打赏
  • 举报
回复
引用 4 楼 feiyun0112 的回复:
试试WM_IME_COMPOSITION


这个会每按一个键就触发一次!

if (m.Msg == WM_IME_CHAR)
{

}
if (m.Msg == WM_IME_COMPOSITION) {
Debug.WriteLine("W:" + m.WParam.ToInt32() + " L:" + m.LParam.ToInt32());
StringBuilder str = new StringBuilder();
int size = ImmGetCompositionString(imeHandle, GCS_RESULTSTR, null, 0);
Debug.WriteLine("size:" + size);
size += sizeof(Char);
ImmGetCompositionString(imeHandle, GCS_RESULTSTR, str, size);
Debug.WriteLine(str.ToString().Substring(0,str.Length));
}



看图:图为用拼音输入法打一个“我”字.

feiyun0112 2014-02-27
  • 打赏
  • 举报
回复
试试WM_IME_COMPOSITION
Bob66666 2014-02-27
  • 打赏
  • 举报
回复
引用 2 楼 feiyun0112 的回复:
WM_IME_CHAR这个每输入一个字是不是就触发一次? ***************************************************************************** 签名档: http://feiyun0112.cnblogs.com/
貌似是一个汉字就发送一次消息,通常我们不是打一个字就按空格键,而是打一串字才按空格,这一串字会被重复发送,怎办?
feiyun0112 2014-02-27
  • 打赏
  • 举报
回复
WM_IME_CHAR这个每输入一个字是不是就触发一次?

*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
Bob66666 2014-02-27
  • 打赏
  • 举报
回复
补充 头部文件:

 public const int WM_NCVATIVATE = 0x86;
        public const int WM_NCPAINT = 0x0085;
        public const int WM_NCCALCSIZE = 0x83;
        public const int WM_NCHITTEST = 0X84;
        public const int WM_CHAR=0x102;
        public const int GCS_RESULTSTR = 0x800;
        public const int WM_IME_SETCONTEXT = 0x281;
        public const int WM_IME_CHAR = 0x286;
        [DllImport("user32.dll")]
        static extern IntPtr GetWindowDC(IntPtr handle);
        [DllImport("imm32.dll")]
        static extern IntPtr ImmGetContext(IntPtr handle);
        [DllImport("imm32.dll")]
        static extern bool ImmReleaseContext(IntPtr handle,IntPtr imc);
        [DllImport("imm32.dll")]
        static extern int ImmGetCompositionString(IntPtr imHandle,int dwIndex,StringBuilder buf,int len);
        [DllImport("imm32.dll")]
        static extern int ImmAssociateContext(IntPtr handle, IntPtr imeHandle);
protected override void WndProc(ref Message m)
        {

111,111

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧