稍微晚了一些,我将改进 tasaki 的示例,以便完整地展示我在 Windows 8 平板电脑的 WPF 应用程序中启用 gotFocus/LostFocus 事件时如何实现文本框的显示/隐藏。我希望这能帮助那些有类似烦恼的人,因为如果您想使用触摸事件滚动,则禁用 InkHelper 不是一个很好的选择...
首先,您必须将以下引用添加到 App.Xaml.cs 文件中。
using System.Management;
using System.Runtime.InteropServices;
代码如下:
protected override void OnStartup(StartupEventArgs eventArgs)
{
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotFocusEvent,
new RoutedEventHandler(GotFocus_Event), true);
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.LostFocusEvent,
new RoutedEventHandler(LostFocus_Event), true);
MainApplication.Show();
}
private static void GotFocus_Event(object sender, RoutedEventArgs e)
{
OpenWindows8TouchKeyboard(sender, e);
}
private static bool IsSurfaceKeyboardAttached()
{
SelectQuery Sq = new SelectQuery("Win32_Keyboard");
ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
return osDetailsCollection.Count <= 1 ? true : false;
}
private static void OpenWindows8TouchKeyboard(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null && IsSurfaceKeyboardAttached())
{
var path = @"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe";
if (!File.Exists(path))
{
path = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\osk.exe";
}
Process.Start(path);
textBox.BringIntoView();
}
}
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public const int SC_MINIMIZE = 0xF020;
private void CloseOnscreenKeyboard()
{
int iHandle = FindWindow("IPTIP_Main_Window", "");
if (iHandle > 0)
{
SendMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
}
}
private void LostFocus_Event(object sender, EventArgs e)
{
CloseOnscreenKeyboard();
}