我的触摸屏键盘是高度可定制的界面,除了发送按键之外,拥有我需要的所有组件,任何人都会看到这个问题。最初,当我创建它时,我打算使用 Forms.SendKeys.Send() 但它是一个 WPF 应用程序......不行。首先,VB.Net以其无限的智慧决定它不会处理默认表单消息。去算算吧。
也许,这是我真正的问题,我无法让 WPF 应用程序停止获得焦点。我希望它像 Windows 触摸键盘一样工作,但是透明 WPF 中发生的每个操作都会使该应用程序成为活动应用程序。我希望事件仍然发生,但我需要活动窗口是您想要输入内容的窗口,例如记事本。
关于我应该做什么,使我的 WPF 不可聚焦并将键盘按钮发送到聚焦(或其他)窗口的任何建议?
PS,我在 Vb.Net 的 Visual Studio 2010 中使用 WPF(我可以使用 C# 代码!)
我想你会在这里找到答案有用
有很多复杂的方法可以实现这一目标,但我提出的解决方案是上面的链接很简单,只有一个怪癖,老实说可以解决。拖动输入窗口时,它不会提供反馈,直到移动完成,但您可以通过处理一些非客户端消息来解决此问题,如果您需要,我可以花一些时间查看解决方法,但首先确认此解决方案是正确的为你。
更新:如何将上述方法应用于 WPF 表单的示例。
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Runtime.InteropServices;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
const int WS_EX_NOACTIVATE = 0x08000000;
const int GWL_EXSTYLE = -20;
[DllImport("user32", SetLastError = true)]
private extern static int GetWindowLong(IntPtr hwnd, int nIndex);
[DllImport("user32", SetLastError = true)]
private extern static int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewValue);
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WindowInteropHelper wih = new WindowInteropHelper(this);
int exstyle = GetWindowLong(wih.Handle, GWL_EXSTYLE);
exstyle |= WS_EX_NOACTIVATE;
SetWindowLong(wih.Handle, GWL_EXSTYLE, exstyle);
}
}
}