zoukankan      html  css  js  c++  java
  • WPF中Popup上的textbox无法切换到中文输入法

    As Marco Zhou has said in the msdn forum (http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b2428b85-adc9-4a1e-a588-8dbb3b9aac06/):
    Windows will only send WM_IME_SETCONTEXT message to the active window, Popup by default is designed to be shown with WM_EX_NOACTIVE which means that it doesn't in active state when displaying, that's why IME could not work correctly in this regard, to workaround this issue, you could try set the Popup as the active window using win32 SetActiveWindow() API.

    So the workaround is use MyPopup instead of Popup:

    public class MyPopup : Popup
    {
        [DllImport("user32.dll")]
        static extern IntPtr SetActiveWindow(IntPtr hWnd);

        static MyPopup()
        {
            EventManager.RegisterClassHandler(
                typeof(MyPopup),
                Popup.PreviewGotKeyboardFocusEvent,
                new KeyboardFocusChangedEventHandler(OnPreviewGotKeyboardFocus),
                true);
        }

        private static void OnPreviewGotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
        {
            var textBox = e.NewFocus as TextBoxBase;
            if (textBox != null)
            {
                var hwndSource = PresentationSource.FromVisual(textBox) as HwndSource;
                if (hwndSource != null)
                {
                    SetActiveWindow(hwndSource.Handle);
                }
            }
        }
    }

    官方解决方案

    使用的话自己建一个类代码如上,控件内local:MyPopup 等同于Popup,亲测好使,实用。

    参考:https://connect.microsoft.com/VisualStudio/feedback/details/389998/wpf-popup-messes-with-ime-switching

  • 相关阅读:
    使用pjsip传输已经编码的视频
    xubuntu14.04下编译pjsip及pjsua2 java
    解决 Python.h:没有那个文件或目录 错误的方法
    HIbernate與不支持boolean的數據庫之間的映射
    js数组的操作
    jQuery中ajax的使用与缓存问题的解决方法
    网络游戏中应用可插拔工厂处理消息
    How to Train Triplet Networks with 100K Identities?
    (转)如何用TensorLayer做目标检测的数据增强
    图像超分辨-IDN
  • 原文地址:https://www.cnblogs.com/Tangcy/p/8118481.html
Copyright © 2011-2022 走看看