如何截获带文本输入的控件的输入法输入结果 C#

原创 2016年05月31日 19:24:15

最近在开发一个串口终端软件,需要实现输入汉字,也就是类似于超级终端那样,输入一个汉字或一串汉字立马就发送到串口。

对于字母,很简单,监控按键就可以,如果是汉字,那就不行了。因为汉字是输入法输入的,文本控件不支持输入法输入产生事件。所以就需要另辟蹊径。

网上找了很久,始终没有找到好的方案,后来

参考了这篇博客点击打开链接

找到了思路,只不过楼主没有把问题完美解决。所以我在他的基础上彻底解决了问题


首先是如何得到输入法的结果

代码如下:

</pre><pre name="code" class="csharp">eGCS_RESULTSTR = 0x0800,
public class GetImeComposition
	{
		[DllImport("Imm32.dll")]
		public static extern IntPtr ImmGetContext(IntPtr hWnd);
		[DllImport("Imm32.dll")]
		public static extern bool ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);
		[DllImport("Imm32.dll", CharSet = CharSet.Unicode)]
		private static extern int ImmGetCompositionStringW(IntPtr hIMC, int dwIndex, byte[] lpBuf, int dwBufLen);
		/*
		 * handle 需要处理的控件的句柄
		 * */
		public string CurrentCompStr(IntPtr handle)
		{
			IntPtr hIMC = ImmGetContext(handle);
			try
			{
				int strLen = ImmGetCompositionStringW(hIMC, (int)emImeGcs.eGCS_RESULTSTR, null, 0);

				if (strLen > 0)
				{
					byte[] buffer = new byte[strLen];
					ImmGetCompositionStringW(hIMC, (int)emImeGcs.eGCS_RESULTSTR, buffer, strLen);
					return Encoding.Unicode.GetString(buffer);
				}
				else
				{
					return string.Empty;
				}
			}
			finally
			{
				ImmReleaseContext(handle, hIMC);
			}
		}
	}

接着是自定义控件

不同的控件需要建立不同的类,这里我用了RichTextBox


public class CmdRichTextBox : RichTextBox
	{
		public delegate void PrecessImeStrCallBack(string str);
		public PrecessImeStrCallBack ProcessIme = null;
		private GetImeComposition getImeStr = new GetImeComposition();
		string imeStr;

		protected override void WndProc(ref Message m)
		{
			switch (m.Msg)
			{
				//判断系统消息的ID号 
				//case (int)emWinMsg.eWM_TIMECHANGE:
				//case (int)emWinMsg.eWM_IME_STARTCOMPOSITION:
				//case (int)emWinMsg.eWM_IME_ENDCOMPOSITION:
				case (int)emWinMsg.eWM_IME_COMPOSITION:
				//case (int)emWinMsg.eWM_IME_KEYLAST:
				//case (int)emWinMsg.eWM_TIMER:
				case (int)emWinMsg.eWM_IME_SETCONTEXT:
				//case (int)emWinMsg.eWM_IME_NOTIFY:
				//case (int)emWinMsg.eWM_IME_CONTROL:
				//case (int)emWinMsg.eWM_IME_COMPOSITIONFULL:
				//case (int)emWinMsg.eWM_IME_SELECT:
				//case (int)emWinMsg.eWM_IME_CHAR:
				//case (int)emWinMsg.eWM_IME_REQUEST:
				//case (int)emWinMsg.eWM_IME_KEYDOWN:
				//case (int)emWinMsg.eWM_IME_KEYUP:
					break;
				case (int)emWinMsg.eWM_IME_ENDCOMPOSITION:
					imeStr = getImeStr.CurrentCompStr(this.Handle);
					//Debug.WriteLine(imeStr);
					if (ProcessIme != null)
					{
						ProcessIme(imeStr);
					}
					break;
				default:
					base.WndProc(ref m);
					break;
			}
		}
	}

附上枚举值定义


public enum emWinMsg
	{
		eWM_NULL = 0x0000,
		eWM_CREATE = 0x0001,
		eWM_DESTROY = 0x0002,
		eWM_MOVE = 0x0003,
		eWM_SIZE = 0x0005,
		eWM_ACTIVATE = 0x0006,
		eWM_SETFOCUS = 0x0007,
		eWM_KILLFOCUS = 0x0008,
		eWM_ENABLE = 0x000A,
		eWM_SETREDRAW = 0x000B,
		eWM_SETTEXT = 0x000C,
		eWM_GETTEXT = 0x000D,
		eWM_GETTEXTLENGTH = 0x000E,
		eWM_PAINT = 0x000F,
		eWM_CLOSE = 0x0010,
		eWM_QUERYENDSESSION = 0x0011,
		eWM_QUIT = 0x0012,
		eWM_QUERYOPEN = 0x0013,
		eWM_ERASEBKGND = 0x0014,
		eWM_SYSCOLORCHANGE = 0x0015,
		eWM_ENDSESSION = 0x0016,
		eWM_SHOWWINDOW = 0x0018,
		eWM_CTLCOLOR = 0x0019,
		eWM_WININICHANGE = 0x001A,
		eWM_SETTINGCHANGE = 0x001A,
		eWM_DEVMODECHANGE = 0x001B,
		eWM_ACTIVATEAPP = 0x001C,
		eWM_FONTCHANGE = 0x001D,
		eWM_TIMECHANGE = 0x001E,
		eWM_CANCELMODE = 0x001F,
		eWM_SETCURSOR = 0x0020,
		eWM_MOUSEACTIVATE = 0x0021,
		eWM_CHILDACTIVATE = 0x0022,
		eWM_QUEUESYNC = 0x0023,
		eWM_GETMINMAXINFO = 0x0024,
		eWM_PAINTICON = 0x0026,
		eWM_ICONERASEBKGND = 0x0027,
		eWM_NEXTDLGCTL = 0x0028,
		eWM_SPOOLERSTATUS = 0x002A,
		eWM_DRAWITEM = 0x002B,
		eWM_MEASUREITEM = 0x002C,
		eWM_DELETEITEM = 0x002D,
		eWM_VKEYTOITEM = 0x002E,
		eWM_CHARTOITEM = 0x002F,
		eWM_SETFONT = 0x0030,
		eWM_GETFONT = 0x0031,
		eWM_SETHOTKEY = 0x0032,
		eWM_GETHOTKEY = 0x0033,
		eWM_QUERYDRAGICON = 0x0037,
		eWM_COMPAREITEM = 0x0039,
		eWM_GETOBJECT = 0x003D,
		eWM_COMPACTING = 0x0041,
		eWM_COMMNOTIFY = 0x0044,
		eWM_WINDOWPOSCHANGING = 0x0046,
		eWM_WINDOWPOSCHANGED = 0x0047,
		eWM_POWER = 0x0048,
		eWM_COPYDATA = 0x004A,
		eWM_CANCELJOURNAL = 0x004B,
		eWM_NOTIFY = 0x004E,
		eWM_INPUTLANGCHANGEREQUEST = 0x0050,
		eWM_INPUTLANGCHANGE = 0x0051,
		eWM_TCARD = 0x0052,
		eWM_HELP = 0x0053,
		eWM_USERCHANGED = 0x0054,
		eWM_NOTIFYFORMAT = 0x0055,
		eWM_CONTEXTMENU = 0x007B,
		eWM_STYLECHANGING = 0x007C,
		eWM_STYLECHANGED = 0x007D,
		eWM_DISPLAYCHANGE = 0x007E,
		eWM_GETICON = 0x007F,
		eWM_SETICON = 0x0080,
		eWM_NCCREATE = 0x0081,
		eWM_NCDESTROY = 0x0082,
		eWM_NCCALCSIZE = 0x0083,
		eWM_NCHITTEST = 0x0084,
		eWM_NCPAINT = 0x0085,
		eWM_NCACTIVATE = 0x0086,
		eWM_GETDLGCODE = 0x0087,
		eWM_SYNCPAINT = 0x0088,
		eWM_NCMOUSEMOVE = 0x00A0,
		eWM_NCLBUTTONDOWN = 0x00A1,
		eWM_NCLBUTTONUP = 0x00A2,
		eWM_NCLBUTTONDBLCLK = 0x00A3,
		eWM_NCRBUTTONDOWN = 0x00A4,
		eWM_NCRBUTTONUP = 0x00A5,
		eWM_NCRBUTTONDBLCLK = 0x00A6,
		eWM_NCMBUTTONDOWN = 0x00A7,
		eWM_NCMBUTTONUP = 0x00A8,
		eWM_NCMBUTTONDBLCLK = 0x00A9,
		eWM_KEYDOWN = 0x0100,
		eWM_KEYUP = 0x0101,
		eWM_CHAR = 0x0102,
		eWM_DEADCHAR = 0x0103,
		eWM_SYSKEYDOWN = 0x0104,
		eWM_SYSKEYUP = 0x0105,
		eWM_SYSCHAR = 0x0106,
		eWM_SYSDEADCHAR = 0x0107,
		eWM_KEYLAST = 0x0108,
		eWM_IME_STARTCOMPOSITION = 0x010D,
		eWM_IME_ENDCOMPOSITION = 0x010E,
		eWM_IME_COMPOSITION = 0x010F,
		eWM_IME_KEYLAST = 0x010F,
		eWM_INITDIALOG = 0x0110,
		eWM_COMMAND = 0x0111,
		eWM_SYSCOMMAND = 0x0112,
		eWM_TIMER = 0x0113,
		eWM_HSCROLL = 0x0114,
		eWM_VSCROLL = 0x0115,
		eWM_INITMENU = 0x0116,
		eWM_INITMENUPOPUP = 0x0117,
		eWM_MENUSELECT = 0x011F,
		eWM_MENUCHAR = 0x0120,
		eWM_ENTERIDLE = 0x0121,
		eWM_MENURBUTTONUP = 0x0122,
		eWM_MENUDRAG = 0x0123,
		eWM_MENUGETOBJECT = 0x0124,
		eWM_UNINITMENUPOPUP = 0x0125,
		eWM_MENUCOMMAND = 0x0126,
		eWM_CTLCOLORWinMsgBOX = 0x0132,
		eWM_CTLCOLOREDIT = 0x0133,
		eWM_CTLCOLORLISTBOX = 0x0134,
		eWM_CTLCOLORBTN = 0x0135,
		eWM_CTLCOLORDLG = 0x0136,
		eWM_CTLCOLORSCROLLBAR = 0x0137,
		eWM_CTLCOLORSTATIC = 0x0138,
		eWM_MOUSEMOVE = 0x0200,
		eWM_LBUTTONDOWN = 0x0201,
		eWM_LBUTTONUP = 0x0202,
		eWM_LBUTTONDBLCLK = 0x0203,
		eWM_RBUTTONDOWN = 0x0204,
		eWM_RBUTTONUP = 0x0205,
		eWM_RBUTTONDBLCLK = 0x0206,
		eWM_MBUTTONDOWN = 0x0207,
		eWM_MBUTTONUP = 0x0208,
		eWM_MBUTTONDBLCLK = 0x0209,
		eWM_MOUSEWHEEL = 0x020A,
		eWM_PARENTNOTIFY = 0x0210,
		eWM_ENTERMENULOOP = 0x0211,
		eWM_EXITMENULOOP = 0x0212,
		eWM_NEXTMENU = 0x0213,
		eWM_SIZING = 0x0214,
		eWM_CAPTURECHANGED = 0x0215,
		eWM_MOVING = 0x0216,
		eWM_DEVICECHANGE = 0x0219,
		eWM_MDICREATE = 0x0220,
		eWM_MDIDESTROY = 0x0221,
		eWM_MDIACTIVATE = 0x0222,
		eWM_MDIRESTORE = 0x0223,
		eWM_MDINEXT = 0x0224,
		eWM_MDIMAXIMIZE = 0x0225,
		eWM_MDITILE = 0x0226,
		eWM_MDICASCADE = 0x0227,
		eWM_MDIICONARRANGE = 0x0228,
		eWM_MDIGETACTIVE = 0x0229,
		eWM_MDISETMENU = 0x0230,
		eWM_ENTERSIZEMOVE = 0x0231,
		eWM_EXITSIZEMOVE = 0x0232,
		eWM_DROPFILES = 0x0233,
		eWM_MDIREFRESHMENU = 0x0234,
		eWM_IME_SETCONTEXT = 0x0281,
		eWM_IME_NOTIFY = 0x0282,
		eWM_IME_CONTROL = 0x0283,
		eWM_IME_COMPOSITIONFULL = 0x0284,
		eWM_IME_SELECT = 0x0285,
		eWM_IME_CHAR = 0x0286,
		eWM_IME_REQUEST = 0x0288,
		eWM_IME_KEYDOWN = 0x0290,
		eWM_IME_KEYUP = 0x0291,
		eWM_MOUSEHOVER = 0x02A1,
		eWM_MOUSELEAVE = 0x02A3,
		eWM_CUT = 0x0300,
		eWM_COPY = 0x0301,
		eWM_PASTE = 0x0302,
		eWM_CLEAR = 0x0303,
		eWM_UNDO = 0x0304,
		eWM_RENDERFORMAT = 0x0305,
		eWM_RENDERALLFORMATS = 0x0306,
		eWM_DESTROYCLIPBOARD = 0x0307,
		eWM_DRAWCLIPBOARD = 0x0308,
		eWM_PAINTCLIPBOARD = 0x0309,
		eWM_VSCROLLCLIPBOARD = 0x030A,
		eWM_SIZECLIPBOARD = 0x030B,
		eWM_ASKCBFORMATNAME = 0x030C,
		eWM_CHANGECBCHAIN = 0x030D,
		eWM_HSCROLLCLIPBOARD = 0x030E,
		eWM_QUERYNEWPALETTE = 0x030F,
		eWM_PALETTEISCHANGING = 0x0310,
		eWM_PALETTECHANGED = 0x0311,
		eWM_HOTKEY = 0x0312,
		eWM_PRINT = 0x0317,
		eWM_PRINTCLIENT = 0x0318,
		eWM_HANDHELDFIRST = 0x0358,
		eWM_HANDHELDLAST = 0x035F,
		eWM_AFXFIRST = 0x0360,
		eWM_AFXLAST = 0x037F,
		eWM_PENWINFIRST = 0x0380,
		eWM_PENWINLAST = 0x038F,
		eWM_APP = 0x8000,
		eWM_USER = 0x0400,
		eWM_REFLECT = eWM_USER + 0x1c00
	}


更简单,更有效,更实用的C# 输入法控制

     最近因为,项目需要对输入法进行控制,但进行了多种尝试,总有不如意的地方!    如果运用控件ImeMode属性或者网上找的一些方法,要嘛开发的时候太繁琐,要嘛就是控制不到位!经过反复的尝试,...
  • DevSam
  • DevSam
  • 2008年08月05日 13:05
  • 719

C#在应用程序中精确切换输入法介绍

如果在你的程序里需要输入很多的内容,比如各类单据,如果在进入每一个录入框的时候都能自动把输入法切换到合适的状态将会是一个很酷的特性,相比炫丽的界面而言打字到手抽筋的录入人员们对此会更加感兴趣。在win...
  • wangmj518
  • wangmj518
  • 2013年06月03日 10:17
  • 1836

输入法之语料选择

影响输入法体验的有两块,候选命中以及视觉交互,视觉交互就是看到的键盘以及用户的使用的方便程度,尤其是在手机输入法中,键盘布局以及一些默认设置等细节对体验影响很大。候选命中就是指用户输入拼音时,能否把用...
  • hxxiaopei
  • hxxiaopei
  • 2014年06月09日 14:22
  • 7332

Mac 输入法候选词框消失的解决方法

1、在某些情况下,你可能会发现mac输入法的候选框消失了,如下图所示 2、出现这种情况,系统重启后会消失,但是重启毕竟很麻烦,这个时候,可以尝试以下方法解决该问题: A:打开[活动监视器] ...
  • ElonLink
  • ElonLink
  • 2016年07月28日 20:31
  • 12412

.Net WinForm 中关于输入法打开却无法输入中文总结

  根据前面的兄弟们解决方法我做了下总结,希望对以后遇到此问题的同行提供点帮助,大家如果还有好的方法也请回复提供我。共同学习。   出现这个问题时我的输入法设置为    注...
  • liuying8448
  • liuying8448
  • 2015年04月20日 11:33
  • 4821

C#控件控制输入文本长度

C#在控制控件输入文本的长度时要注意两个问题: 1、传递的事件参数类型要是 KeyPressEventArgs; 2、对退格键(backspace)做例外处理,不然在输入到最大程度时无法使用退格键...
  • dou3516
  • dou3516
  • 2017年01月02日 15:48
  • 554

C# 自定义控件,日期时间选择输入插件

C# 自定义控件,日期时间选择输入插件
  • scimence
  • scimence
  • 2016年06月14日 18:12
  • 7301

输入法信息捕获总结

    输入法信息捕获相关的消息有WM_IME_SETCONTEXT、WM_IME_SELECT和WM_IME_NOTIFY。WM_IME_SETCONTEXT为文本框激活后输入法上下文激活的消息,W...
  • Matrix_Designer
  • Matrix_Designer
  • 2011年05月05日 12:01
  • 2350

Android中的文本输入

首先在XML中添加一个TextView:
  • renhong20121314
  • renhong20121314
  • 2016年07月14日 10:00
  • 406

Qt qml TextField TextInput等文本输入控件中validator属性的设置

一 环境 桌面系统: Ubuntu16.04  32位 Qt版本:5.5.1 二 限制输入框只输入数字(通过设置 validator) 1  IntValidator  作用:限制输...
  • Garvie
  • Garvie
  • 2017年06月13日 19:49
  • 1345
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何截获带文本输入的控件的输入法输入结果 C#
举报原因:
原因补充:

(最多只允许输入30个字)