zoukankan      html  css  js  c++  java
  • WPF与输入法冲突研究之三:韩文输入法在不同平台,WinForm/WPF下的区别

    输入法的问题,已经把我折腾的精疲力尽了。。终于在Win7平台上,中日韩三国输入法能够在WPF上正确使用之后,WinXP又不行了。。。韩文输入法无法正确使用。。。

    好吧,只能做几个小程序,测试下韩文输入法在不同平台,不同框架(WinForm/WPF)下的区别:


    1. 韩文在输入过程中,很奇特,会有一个高亮闪烁的小方块,这个表示,当前的韩文字符正在构造中,他会随着你之后的输入而变化,这个高亮的小方块,有什么奇特的呢?在WPF的TextBox里面,TextBox的SelectionLength属性,返回的是1(表示有一个字符被选中),而在WinForm下面,返回的是0(表示没有字符被选中)。这个在Win7/WinXP平台下,都是一致的。看截图:


    Win7 WPF/WinForm (输入t,组出来的韩文应该是ㅅ,注意Selection Length和Selection Start):


    WinXP WPF/WinForm: (XP下WinForm的小方块有点小。。。)



    好,每个UI都有一个CheckBox,这个的意思就是程序每个10毫秒会自动调用txtBox.Select(txtBox.SelectionStart, 0);语句,这个有什么意义呢?这句语句相当于把Selection清除。我就想看看,在清除了Selection之后,韩文输入法能否正常工作。经测试,2个平台下WinForm都能正常工作,而WPF只有在Win7平台才能正常工作,在XP下,韩文输入法有2个大问题:一个是不能正常组字,二就是光标会跑到输入的字符前面。。。看截图:


    Win7 WPF/WinForm (输入ty,组出来的韩文应该是쇼注意Auto Call已经开启,这时候Selection Length都变成了0),Win7下,仍然有个小方块,而且组出来的字是正确的。Winform把t和ty组出来的字都加到了TextBox里!



    WinXP WPF/WinForm (注意Auto Call已经开启,这时候Selection Length都变成了0),WPF没有组字,直接把t和y变成了2个字符,而且第二个字符插在了第一个字符之前。WinForm和Win7平台一样:



    总结:



    附上代码:

    Wpf:

    using System;
    using System.Windows;
    using System.Windows.Interop;
    using System.Windows.Threading;
    
    namespace Wpf.ImeSelectionTracker
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                new DispatcherTimer(TimeSpan.FromSeconds(0.01), DispatcherPriority.Normal,
        (sender, e) => { UpdateImeState(); }, this.Dispatcher);
    
            }
    
            private void UpdateImeState()
            {
                lblSelectionStart.Content = "N/A";
                lblSelectionLength.Content = "N/A";
                lblSelectionString.Content = "N/A";
                try
                {
                    IntPtr hwndTextBox = (HwndSource.FromVisual(this.txtBox) as HwndSource).Handle;
    
                    lblSelectionStart.Content = txtBox.SelectionStart;
                    lblSelectionLength.Content = txtBox.SelectionLength;
                    lblSelectionString.Content = txtBox.Text.Substring(txtBox.SelectionStart, txtBox.SelectionLength);
                    if(chkAutoSelection.IsChecked == true)
                        txtBox.Select(txtBox.SelectionStart, 0);
                }
                catch { }
            }
        }
    }
    

    WinForm:


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WinForm.ImeSelectionTracker
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                Timer timer = new Timer();
                timer.Interval = 10;
                timer.Tick += (sender, e) => { UpdateImeState(); };
                timer.Start();
            }
    
            private void UpdateImeState()
            {
                lblSelectionStart.Text = "N/A";
                lblSelectionLength.Text = "N/A";
                lblSelectionString.Text = "N/A";
    
                try
                {
                    IntPtr hwndTextBox = this.txtBox.Handle;
    
                    lblSelectionStart.Text = txtBox.SelectionStart.ToString();
                    lblSelectionLength.Text = txtBox.SelectionLength.ToString();
                    lblSelectionString.Text = txtBox.Text.Substring(txtBox.SelectionStart, txtBox.SelectionLength);
    
                    if (chkBox.Checked)
                        txtBox.Select(txtBox.SelectionStart, 0);
    
                }
                catch { }
    
            }
    
        }
    }
    







  • 相关阅读:
    Mini2440裸机开发之MMU
    Mini2440裸机开发之IIC
    Mini2440裸机开发之SPI
    Mini2440裸机开发之存储器控制器
    Mini2440裸机开发之模数转换开发
    Mini2440裸机开发之LCD编程(GB2312、ASCII字库制作)
    Python pandas df.iloc[:, 0] 取确定列值作双轴图
    python plot 画双坐标轴 设置百分比展示 和字体等细节
    python Sqlserver数据库画双轴图
    windows下Idea中用Python访问oracle数据库的方法
  • 原文地址:https://www.cnblogs.com/puncha/p/3876950.html
Copyright © 2011-2022 走看看