/// IntPtr handle is the handle to the textbox public string CurrentCompStr(IntPtr handle) { int readType = GCS_COMPSTR; IntPtr hIMC = IMM.ImmGetContext(handle); try { int strLen = IMM.ImmGetCompositionStringW(hIMC, readType, null, 0); if (strLen > 0) { byte[] buffer = new byte[strLen]; IMM.ImmGetCompositionStringW(hIMC, readType, buffer, strLen); return(Encoding.Unicode.GetString(buffer)); } else { return(string.Empty); } } finally { IMM.ImmReleaseContext(handle, hIMC); } }
public void OpenIME() { IntPtr hwndInput = IMM.ImmGetContext(IMM.GetActiveWindow()); IMM.ImmSetOpenStatus(hwndInput, true); IntPtr dw1 = IntPtr.Zero; IntPtr dw2 = IntPtr.Zero; bool isSuccess = IMM.ImmGetConversionStatus(hwndInput, ref dw1, ref dw2); Log.Info("" + hwndInput + "," + isSuccess); if (isSuccess) { int intTemp = dw1.ToInt32() & IME_CMODE_SOFTKBD; if (intTemp > 0) { dw1 = (IntPtr)(dw1.ToInt32() ^ IME_CMODE_SOFTKBD); } else { dw1 = (IntPtr)(dw1.ToInt32() | IME_CMODE_SOFTKBD); } } isSuccess = IMM.ImmSetConversionStatus(hwndInput, dw1, dw2); IMM.ImmReleaseContext(IMM.GetActiveWindow(), hwndInput); }
private void IMESetContext(ref Message msg) { if (msg.WParam.ToInt32() == 1) { IntPtr ptr = IMM.ImmGetContext(Handle); if (_context == IntPtr.Zero) { _context = ptr; } else if (ptr == IntPtr.Zero && IsEnabled) { enableIME(); } _compcurpos.IMEHandle = _context; _compstr.IMEHandle = _context; _compclause.IMEHandle = _context; _compattr.IMEHandle = _context; _compread.IMEHandle = _context; _compreadclause.IMEHandle = _context; _compreadattr.IMEHandle = _context; _resstr.IMEHandle = _context; _resclause.IMEHandle = _context; _resread.IMEHandle = _context; _resreadclause.IMEHandle = _context; if (!_showIMEWin) { msg.LParam = (IntPtr)0; } } }
protected override void WndProc(ref Message message) { if (message.Msg == WindowMessage.InputLanguageChange) { return; } if (message.Msg == WindowMessage.ImeSetContext) { if (message.WParam.ToInt32() == 1) { IntPtr imeContext = IMM.ImmGetContext(this.Handle); if (context == IntPtr.Zero) { context = imeContext; } IMM.ImmAssociateContext(this.Handle, context); } } base.WndProc(ref message); switch (message.Msg) { case (int)WindowMessages.WM_GETDLGCODE: { if (Is32Bit) { int returnCode = message.Result.ToInt32(); returnCode |= (DLGC_WANTALLKEYS | DLGC_WANTCHARS); message.Result = new IntPtr(returnCode); } else { long returnCode = message.Result.ToInt64(); returnCode |= (DLGC_WANTALLKEYS | DLGC_WANTCHARS); message.Result = new IntPtr(returnCode); } break; } case (int)WindowMessages.WM_CHAR: { int charInt = message.WParam.ToInt32(); Character myCharacter = new Character(); myCharacter.IsUsed = false; myCharacter.Chars = (char)charInt; //汉字的unicode编码范围是4e00-9fa5(19968至40869) //全/半角标点可以查看charInt输出 switch (charInt) { case 8: myCharacter.CharacterType = (int)CharacterType.BackSpace; break; case 9: myCharacter.CharacterType = (int)CharacterType.Tab; break; case 13: myCharacter.CharacterType = (int)CharacterType.Enter; break; case 27: myCharacter.CharacterType = (int)CharacterType.Esc; break; default: myCharacter.CharacterType = (int)CharacterType.Char; break; } myCharacters.Add(myCharacter); break; } } }