/// 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 IMEChangeCandidate() { uint length = IMM.ImmGetCandidateList(_context, 0, IntPtr.Zero, 0); if (length > 0) { IntPtr pointer = Marshal.AllocHGlobal((int)length); length = IMM.ImmGetCandidateList(_context, 0, pointer, length); IMM.CandidateList cList = (IMM.CandidateList)Marshal.PtrToStructure(pointer, typeof(IMM.CandidateList)); CandidatesSelection = cList.dwSelection; CandidatesPageStart = cList.dwPageStart; CandidatesPageSize = cList.dwPageSize; if (cList.dwCount > 1) { Candidates = new string[cList.dwCount]; for (int i = 0; i < cList.dwCount; i++) { int sOffset = Marshal.ReadInt32(pointer, 24 + 4 * i); Candidates[i] = Marshal.PtrToStringUni((IntPtr)(pointer.ToInt32() + sOffset)); } if (onCandidatesReceived != null) { onCandidatesReceived(this, EventArgs.Empty); } } else { IMECloseCandidate(); } Marshal.FreeHGlobal(pointer); } }
/// <summary> /// Get IME Chinese/English Status /// </summary> /// <returns></returns> public bool getStatus() { IntPtr dw1 = IntPtr.Zero; IntPtr dw2 = IntPtr.Zero; return(IMM.ImmGetConversionStatus(Handle, ref dw1, ref dw2)); }
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; } } }
internal override void Update() { Length = IMM.ImmGetCompositionString(IMEHandle, Flag, IntPtr.Zero, 0); IntPtr pointer = Marshal.AllocHGlobal(Length); try { IMM.ImmGetCompositionString(IMEHandle, Flag, pointer, Length); _values = new byte[Length]; Marshal.Copy(pointer, _values, 0, Length); } finally { Marshal.FreeHGlobal(pointer); } }
public bool PreFilterMessage(ref Message m) { switch (m.Msg) { case IMM.KeyDown: IntPtr intPtr = Marshal.AllocHGlobal(Marshal.SizeOf(m)); Marshal.StructureToPtr(m, intPtr, true); IMM.TranslateMessage(intPtr); return(false); case 0x020A: // Mouse wheel is not correct if the IME helper is used, thus it is needed to grab the value here. MouseWheel += (int)(short)((uint)(int)m.WParam >> 16); return(false); } return(false); }
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; } } }
private bool ImeGetOpenStatus(IntPtr lParam) { return(IMM.ImmGetOpenStatus(lParam)); }
/// <summary> /// Disable the IME /// </summary> public void disableIME() { IsEnabled = false; IMM.ImmAssociateContext(Handle, IntPtr.Zero); }
/// <summary> /// Enable the IME /// </summary> public void enableIME() { IsEnabled = true; IMM.ImmAssociateContext(Handle, _context); }
internal override void Update() { Value = IMM.ImmGetCompositionString(IMEHandle, Flag, IntPtr.Zero, 0); }