输入法的问题,已经把我折腾的精疲力尽了。。终于在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 { } } } }