private void ClearContext() { if (_hwndSource == null) { return; } ImeNative.ImmAssociateContext(_hwndSource.Handle, _previousContext); ImeNative.ImmReleaseContext(_defaultImeWnd, _currentContext); _currentContext = IntPtr.Zero; _defaultImeWnd = IntPtr.Zero; _hwndSource.RemoveHook(WndProc); _hwndSource = null; }
private void CreateContext() { _hwndSource = (HwndSource)PresentationSource.FromVisual(_textArea); if (_hwndSource == null) { return; } _defaultImeWnd = ImeNative.ImmGetDefaultIMEWnd(IntPtr.Zero); _currentContext = ImeNative.ImmGetContext(_defaultImeWnd); _previousContext = ImeNative.ImmAssociateContext(_hwndSource.Handle, _currentContext); _hwndSource.AddHook(WndProc); //尽管文档说传递null是无效的,但这似乎有助于在与WPF共享的默认输入上下文中激活IME var threadMgr = ImeNative.GetTextFrameworkThreadManager(); threadMgr?.SetFocus(IntPtr.Zero); }