Wpf画面处理时,遇到个问题,popup上怎么都切换不出输入法,只有QQ输入法勉强能切出来打中文,但是也看不到输入框,原来popup上的textbox无法切换出输入法,是微软的一个bug,微软说4.0会修正该bug,项目是3.5的,最新的4.0试了下确实已经修正该问题,可惜用不到。需要自己写个自定义的popup做处理。可能是popup的弹出性质导致的,大概是popup出现时总是处于Active状态,切换输入法时又必须使输入法处于Active状态,以致无法换成切换。代码前半段连语法不会,转不成vb.net,直接c#编译成dll供日后使用算了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Controls.Primitives;
using System.Windows.Interop;
using System.Windows.Input;
using System.Windows;
namespace HDTWpfControls.vb
{
public class HdtPopup : Popup
{
[DllImport("user32.dll")]
static extern IntPtr SetActiveWindow(IntPtr hWnd);
static void HdtPopup()
{
EventManager.RegisterClassHandler(typeof(HdtPopup), 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);
}
}
}
}
}
转自: http://blog.sina.com.cn/s/blog_90231c3a0100wrob.html