WPF - 在离屏渲染网格上模拟鼠标事件
我将具有多个元素(按钮、文本框等)的 WPF 网格渲染为位图,然后将其用作 Direct3D 场景中 3D 表面的纹理。对于用户交互,我创建了一条从 2D 鼠标光标位置到 3D 场景的 3D 射线,找到与 GUI 表面的交点。所以我知道用户在 WPF 网格上单击了哪里,但从那里我陷入了困境:
如何在 WPF 元素上模拟鼠标事件,而它们实际上并未显示在打开的窗口中而是呈现在屏幕外?
最近,我正在研究 UIAutomation 和 RaiseEvent,但它们用于将事件发送到单个元素,而不是整个可视树。手动遍历树并在光标位置查找元素将是一种选择,但我还没有找到准确执行此操作的方法。 VisualTreeHelper.HitTest 是一个好的开始,但它不是找到 TextBox,而是找到 TextBoxView,而是找到 Border,而不是 ListBox。
编辑:在 HitTest 的结果回调中返回 HitTestResultBehavior.Continue 让我可以遍历给定点的所有元素。我现在可以将鼠标事件发送到所有这些元素,但 MouseEventArgs 对象的值是真实鼠标的值。所以我必须创建一个自定义的 MouseDevice,这显然是不可能的。
PointHitTestParameters p = new PointHitTestParameters(new Point(
((Vector2)hit).X * sourceElement.ActualWidth,
(1 - ((Vector2)hit).Y) * sourceElement.ActualHeight));
VisualTreeHelper.HitTest(sourceElement,
new HitTestFilterCallback(delegate(DependencyObject o)
{
return HitTestFilterBehavior.Continue;
}),
new HitTestResultCallback(delegate(HitTestResult r)
{
UIElement el = r.VisualHit as UIElement;
if (el != null)
{
MouseButtonEventArgs e = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left);
if (leftMouseDown) e.RoutedEvent = Mouse.MouseDownEvent;
else e.RoutedEvent = Mouse.MouseUpEvent;
el.RaiseEvent(e);
}
return HitTestResultBehavior.Continue;
}), p);
I'm rendering a WPF grid with multiple elements (buttons, textbox, ...) to a bitmap which is then used as a texture for a 3D surface in a Direct3D scene. For user interaction I create a 3D ray from the 2D mouse cursor position into the 3D scene finding the intersection point with the gui surface. So I know where the user has clicked on the WPF grid, but from there I'm stuck:
How can I simulate mouse events on the WPF elements while they are not actually shown in an open window but rendered off-screen?
Recently, I was looking into UIAutomation and RaiseEvent but these are used to send events to individual elements, not the whole visual tree. Traversing the tree manually and looking for elements at the cursor position would be an option but I haven't found a way to do this accurately. VisualTreeHelper.HitTest is a good start but instead of finding TextBox it finds TextBoxView and instead of ListBox it finds a Border.
EDIT: returning HitTestResultBehavior.Continue in the result callback of HitTest lets me walk through all elements at a given point. I can now send mouse events to all these elements but the values of the MouseEventArgs object are those of the real mouse. So I have to create a custom MouseDevice which apparently is impossible.
PointHitTestParameters p = new PointHitTestParameters(new Point(
((Vector2)hit).X * sourceElement.ActualWidth,
(1 - ((Vector2)hit).Y) * sourceElement.ActualHeight));
VisualTreeHelper.HitTest(sourceElement,
new HitTestFilterCallback(delegate(DependencyObject o)
{
return HitTestFilterBehavior.Continue;
}),
new HitTestResultCallback(delegate(HitTestResult r)
{
UIElement el = r.VisualHit as UIElement;
if (el != null)
{
MouseButtonEventArgs e = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left);
if (leftMouseDown) e.RoutedEvent = Mouse.MouseDownEvent;
else e.RoutedEvent = Mouse.MouseUpEvent;
el.RaiseEvent(e);
}
return HitTestResultBehavior.Continue;
}), p);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过 PostMessage(..) win32 方法将 Windows 消息(如 WM_MOUSEMOVE)发送到 WPF 窗口的 HWND。我相信这些消息将由 WPF 读取并执行,就好像它来自用户一样。
You might be able to send windows messages (like WM_MOUSEMOVE) to the WPF window's HWND, via the PostMessage(..) win32 method. I believe these messages would be read by WPF and executed as if it came from a user.
如果您真的很勇敢,您可以尝试 我的黑客访问 WPF IDirect3DDevice9Ex。从那里您可以将交换链中的后备缓冲区复制到 d3d 场景中。如果您使用 9Ex (Vista D3D9),您可以利用共享资源(在设备和进程之间共享表面/纹理等)功能来帮助提高性能。
您可能仍然需要对 Windows 消息进行一些操作才能实现交互。
If you are feeling really brave, you can try my hack to get access to the WPF IDirect3DDevice9Ex. From there you can copy the backbuffer in the swapshchain to your d3d scene. If you are using 9Ex (Vista D3D9), you can take advantage of the shared resources (share surfaces/textures/etc between devices and processes) feature to help with performance.
You may still have to do some trickery with windows messages for interactivity.