/// <summary> /// </summary> public static String GetImmCompositionString(IntPtr immContext, Int32 dwIndex) { if (immContext == IntPtr.Zero) { return(null); } Int32 dwBufLen = Imm32.ImmGetCompositionStringW(immContext, dwIndex, null, 0); if (dwBufLen < 0) { return(null); } StringBuilder lpBuf = new StringBuilder(dwBufLen / 2); dwBufLen = Imm32.ImmGetCompositionStringW(immContext, dwIndex, lpBuf, dwBufLen); if (dwBufLen < 0) { return(null); } return(lpBuf.ToString().Substring(0, dwBufLen / 2)); }
public static bool IsImeActive(IntPtr windowHandle) { bool isActive = false; try { IntPtr handle = Imm32.ImmGetContext(windowHandle); if (handle == IntPtr.Zero) { return(false); } try { isActive = Imm32.ImmGetOpenStatus(handle); } finally { Imm32.ImmReleaseContext(windowHandle, handle); } return(isActive); } catch (Exception ex) { Trace.Fail("Failed to check if IME is active: " + ex); return(isActive); } }
/// <summary> /// </summary> public static void FlushImmCompositionString(IntPtr immContext) { if (immContext != IntPtr.Zero) { StringBuilder lpComp = new StringBuilder(); Imm32.ImmSetCompositionStringW(immContext, 9, lpComp, 0, lpComp, 0); } }
/// <summary> /// </summary> public static void AttachContext(HwndSource hwndSource) { if (hwndSource == null) { throw new ArgumentNullException("hwndSource"); } IntPtr hIMC = Imm32.ImmCreateContext(); if (hIMC != IntPtr.Zero) { Imm32.ImmAssociateContext(hwndSource.Handle, hIMC); } }
/// <summary> /// </summary> public static void DetachContext(HwndSource hwndSource) { if (hwndSource == null) { throw new ArgumentNullException("hwndSource"); } IntPtr immContext = Imm32.ImmAssociateContext(hwndSource.Handle, IntPtr.Zero); if (immContext != IntPtr.Zero) { Imm32.ImmDestroyContext(immContext); } }
/// <summary> /// </summary> public static IntPtr GetImmContext(Visual visual) { if (visual == null) { throw new ArgumentNullException("visual"); } HwndSource hwndSource = GetHwndSource(visual); if (hwndSource != null) { return(Imm32.ImmGetContext(hwndSource.Handle)); } return(IntPtr.Zero); }
/// <summary> /// </summary> public static Boolean SetImmFontHeight(IntPtr immContext, Int32 fontHeight) { if (immContext == IntPtr.Zero) { return(false); } LOGFONT structure = new LOGFONT(); structure.lfHeight = fontHeight; IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(structure)); Marshal.StructureToPtr(structure, ptr, true); Boolean flag = Imm32.ImmSetCompositionFontW(immContext, ptr); Marshal.FreeHGlobal(ptr); return(flag); }
/// <summary> /// 移动光标到新的光标区域 /// </summary> /// <param name="vLeft">在视图中光标区域的左端位置</param> /// <param name="vTop">在视图中光标区域的顶端位置</param> /// <param name="vWidth">光标区域宽度</param> /// <param name="vHeight">光标区域高度</param> /// <returns>移动光标是否造成滚动</returns> public bool MoveCaretTo(int vLeft, int vTop, int vWidth, int vHeight) { if (this.Updating) { return(false); } if (ForceShowCaret == false && this.Focused == false) { if (bolCaretCreated) { myCaret.Hide(); } return(false); } int height = GraphicsUnitConvert.Convert(vHeight, this.GraphicsUnit, System.Drawing.GraphicsUnit.Pixel); if (vWidth > 0 && vHeight > 0) { bolCaretCreated = myCaret.Create(0, vWidth, height); if (bolCaretCreated) { if (bolMoveCaretWithScroll) { //Modified by wwj 2011-11-24 移除自动判断的滚动条的逻辑 不知道为什么要移除,现在打开看有什么问题 edit by Ukey zhang 2017-11-11-15 //this.ScrollToView(vLeft, vTop, vWidth, vHeight); } System.Drawing.Point p = this.ViewPointToClient(vLeft, vTop); myCaret.SetPos(p.X, p.Y); myCaret.Show(); if (Imm32.isImmOpen(this.Handle.ToInt32())) { Imm32.SetImmPos(this.Handle.ToInt32(), p.X, p.Y); } myCaretBounds = new System.Drawing.Rectangle(p.X, p.Y, vWidth, height); if (bolMoveCaretWithScroll) { return(true); } } } return(false); }
/// <summary> /// </summary> public static Boolean HanjaConversion(Visual visual, IntPtr keyboardLayout, Char selection) { IntPtr immContext = GetImmContext(visual); if (immContext != IntPtr.Zero) { IntPtr lpBuf = Marshal.StringToHGlobalUni(new String(selection, 1)); IntPtr ptr3 = Imm32.ImmEscapeW(keyboardLayout, immContext, 0x1008, lpBuf); Marshal.FreeHGlobal(lpBuf); ReleaseImmContext(visual, immContext); if (ptr3 != IntPtr.Zero) { return(true); } } return(false); }
/// <summary> /// </summary> public static Boolean ReleaseImmContext(Visual visual, IntPtr immContext) { if (visual == null) { throw new ArgumentNullException("visual"); } if (immContext != IntPtr.Zero) { HwndSource hwndSource = GetHwndSource(visual); if (hwndSource != null) { return(Imm32.ImmReleaseContext(hwndSource.Handle, immContext)); } } return(false); }
/// <summary> /// </summary> public static Boolean SetCompositionWindowPosition(IntPtr immContext, Point pt, Visual relativeTo) { if (immContext == IntPtr.Zero) { throw new ArgumentNullException("immContext"); } if (relativeTo == null) { throw new ArgumentNullException("relativeTo"); } Visual rootVisual = GetRootVisual(relativeTo); if (rootVisual == null) { return(false); } Point point = relativeTo.TransformToAncestor(rootVisual).Transform(pt); HwndSource hwndSource = GetHwndSource(rootVisual); if (hwndSource != null) { point = hwndSource.CompositionTarget.TransformToDevice.Transform(point); } COMPOSITIONFORM structure = new COMPOSITIONFORM(); structure.dwStyle = 2; structure.ptCurrentPos = new POINT((Int32)point.X, (Int32)point.Y); IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(structure)); Marshal.StructureToPtr(structure, ptr, true); Boolean flag = Imm32.ImmSetCompositionWindow(immContext, ptr); Marshal.FreeHGlobal(ptr); return(flag); }
/// <summary> /// 取得指定輸入法的 keyboard layout handle。 /// </summary> /// <param name="imeName">輸入法名稱。</param> /// <returns>若成功取得輸入法 handle,則傳回 handle 指標,否則傳回空指標。</returns> public static void InitializeKeyboardLayoutList() { const int MaxHkl = 20; IntPtr[] hklList = new IntPtr[MaxHkl]; int cntHkl; int i; IntPtr imeNamePtr; string imeName; cntHkl = Imm32.GetKeyboardLayoutList(hklList.Length, hklList); if (cntHkl < 1) { return; } imeNamePtr = Marshal.AllocHGlobal(256); try { for (i = 0; i < cntHkl; i++) { if (Imm32.ImmEscape(hklList[i], IntPtr.Zero, Imm32.IME_ESC_IME_NAME, imeNamePtr) == 0) { continue; } imeName = Marshal.PtrToStringUni(imeNamePtr); KeyboardLayoutInfo kli = new KeyboardLayoutInfo(imeName, hklList[i]); _keyboardLayoutList.Add(kli); } } finally { Marshal.FreeHGlobal(imeNamePtr); } }
public static HIMC ImmGetContext(this HWND hWnd) => Imm32.ImmGetContext(hWnd);
public static bool ImmAssociateContextEx(this HWND hWnd, HIMC hIMC, IACE dwFlags) => Imm32.ImmAssociateContextEx(hWnd, hIMC, dwFlags);
public static bool ImmDestroyContext(this HIMC hIMC) => Imm32.ImmDestroyContext(hIMC);
public static (IME_CMODE, IME_SMODE) ImmGetConversionStatus(this HIMC hIMC) { Imm32.ImmGetConversionStatus(hIMC, out var cmode, out var smode); return(cmode, smode); }
public static string ImmGetDescription(this HKL hKL) => BufferReader.ReadString((buf, size) => Imm32.ImmGetDescriptionW(hKL, buf, size));
public static void ImmSetOpenStatus(this HIMC hIMC, bool status) => Imm32.ImmSetOpenStatus(hIMC, status ? -1 : 0);
public static bool ImmGetOpenStatus(this HIMC hIMC) => Imm32.ImmGetOpenStatus(hIMC);
public static CandidateCollection ImmGetCandidateList(this HIMC hIMC) => new CandidateCollection(BufferReader.ReadBytes((buf, size) => Imm32.ImmGetCandidateListW(hIMC, 0, buf, size)));
public static string ImmGetCompositionString(this HIMC hIMC, GCS dwIndex) => BufferReader.ReadString((buf, size) => Imm32.ImmGetCompositionStringW(hIMC, dwIndex, buf, size));
public static bool ImmReleaseContext(this HWND hWnd, HIMC hIMC) => Imm32.ImmReleaseContext(hWnd, hIMC);
/// <summary> /// </summary> public static Boolean ImmNotifyIME(IntPtr immContext, Int32 dwAction, Int32 dwIndex, Int32 dwValue) { return(Imm32.ImmNotifyIME(immContext, dwAction, dwIndex, dwValue)); }