基本上我要做的是有一个窗口,控件响应鼠标和键盘,但没有采取焦点 . 我一直在寻找钩住窗口事件来捕获它们或使用预览事件而不是处理它们,但无论我做什么,窗口都会触发鼠标和键盘事件并获得焦点或根本不做任何事情 .
是否可以手动挂钩窗口的鼠标/键盘事件,同时仍将事件传递给后面的任何窗口(其他应用程序)?
Edit:
我已经尝试了下面的代码来使窗口点击,但这会导致鼠标和键盘事件根本不被触发 .
public const int WS_EX_TRANSPARENT = 0x00000020; public const int GWL_EXSTYLE = (-20);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
protected override void OnSourceInitialized(EventArgs e)
{
// Get this window's handle
IntPtr hwnd = new WindowInteropHelper(this).Handle;
// Change the extended window style to include WS_EX_TRANSPARENT
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
var hwndSource = PresentationSource.FromVisual(this) as HwndSource;
if (hwndSource != null)
hwndSource.CompositionTarget.RenderMode = RenderMode.Default;
base.OnSourceInitialized(e);
}