wpf 如何在不使用触摸屏的情况下模拟触摸事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29489004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I Emulate touch events without using touch screen
提问by Sam
I'm currently trying to develop a touch screen application with:
我目前正在尝试开发一个触摸屏应用程序:
- Windows 7
- Visual Studio 2013
- C# - WPF
- Windows 7的
- 视觉工作室 2013
- C# - WPF
The place I'm working at is going to receive a touch screen (actually a layer to put on a flat screen).
我正在工作的地方将接收一个触摸屏(实际上是一个放置在平面屏幕上的层)。
I would like to be able to generate touch inputs in order to develop and test the application without the screen.
我希望能够生成触摸输入,以便在没有屏幕的情况下开发和测试应用程序。
All the resources I find, are either quite old or complicated.
我找到的所有资源要么很旧,要么很复杂。
What is the best way today to develop and test touch screen applications, without the touch screen ?
在没有触摸屏的情况下,当今开发和测试触摸屏应用程序的最佳方法是什么?
回答by PiotrWolkowski
One way to do it is to attach a second mouse to your work station. Then you can test multitouch (resizing, rotating, etc.). I managed to do that years ago. You need appropriate drivers though. Please check the moultitouch project. I think I was using that or something similar.
一种方法是将第二个鼠标连接到您的工作站。然后您可以测试多点触控(调整大小、旋转等)。几年前我设法做到了。不过,您需要适当的驱动程序。请检查moultitouch 项目。我想我正在使用那个或类似的东西。
You can find more suggestions in this SuperUser post. But I never tried them.
您可以在这篇超级用户帖子中找到更多建议。但我从未尝试过它们。
EDIT
编辑
After checking your comments I understand your issue better. Try this StackOverflow thread. It's discussing rerouting mouse into touch events. Check also Blake.NUIproject - it improves WPF 4 to better handle touch interaction (among other things).
查看您的评论后,我更了解您的问题。试试这个StackOverflow 线程。它正在讨论将鼠标重新路由到触摸事件中。还检查Blake.NUI项目 - 它改进了 WPF 4 以更好地处理触摸交互(除其他外)。
In the project you will find MouseTouchDeviceclass that should help you converting mouse into touch events:
在项目中,您将找到MouseTouchDevice可以帮助您将鼠标转换为触摸事件的类:
/// <summary>
/// Used to translate mouse events into touch events, enabling a unified
/// input processing pipeline.
/// </summary>
/// <remarks>This class originally comes from Blake.NUI - http://blakenui.codeplex.com</remarks>
public class MouseTouchDevice : TouchDevice, ITouchDevice
{
#region Class Members
private static MouseTouchDevice device;
public Point Position { get; set; }
#endregion
#region Public Static Methods
public static void RegisterEvents(FrameworkElement root)
{
root.PreviewMouseDown += MouseDown;
root.PreviewMouseMove += MouseMove;
root.PreviewMouseUp += MouseUp;
root.LostMouseCapture += LostMouseCapture;
root.MouseLeave += MouseLeave;
}
#endregion
#region Private Static Methods
private static void MouseDown(object sender, MouseButtonEventArgs e)
{
if (device != null &&
device.IsActive)
{
device.ReportUp();
device.Deactivate();
device = null;
}
device = new MouseTouchDevice(e.MouseDevice.GetHashCode());
device.SetActiveSource(e.MouseDevice.ActiveSource);
device.Position = e.GetPosition(null);
device.Activate();
device.ReportDown();
}
private static void MouseMove(object sender, MouseEventArgs e)
{
if (device != null &&
device.IsActive)
{
device.Position = e.GetPosition(null);
device.ReportMove();
}
}
private static void MouseUp(object sender, MouseButtonEventArgs e)
{
LostMouseCapture(sender, e);
}
static void LostMouseCapture(object sender, MouseEventArgs e)
{
if (device != null &&
device.IsActive)
{
device.Position = e.GetPosition(null);
device.ReportUp();
device.Deactivate();
device = null;
}
}
static void MouseLeave(object sender, MouseEventArgs e)
{
LostMouseCapture(sender, e);
}
#endregion
#region Constructors
public MouseTouchDevice(int deviceId) :
base(deviceId)
{
Position = new Point();
}
#endregion
#region Overridden methods
public override TouchPointCollection GetIntermediateTouchPoints(IInputElement relativeTo)
{
return new TouchPointCollection();
}
public override TouchPoint GetTouchPoint(IInputElement relativeTo)
{
Point point = Position;
if (relativeTo != null)
{
point = this.ActiveSource.RootVisual.TransformToDescendant((Visual)relativeTo).Transform(Position);
}
Rect rect = new Rect(point, new Size(1, 1));
return new TouchPoint(this, point, rect, TouchAction.Move);
}
#endregion
}
回答by Offa
回答by Chef Pharaoh
The only way I know of how to do this in Windows 7 (that wouldn't be considered a "hack") is to create a HID digitizer driver and then sending report messages to that driver, which would tell Windows to create the touch events in the specified manner.
我知道如何在 Windows 7 中执行此操作的唯一方法(不会被视为“黑客”)是创建一个 HID 数字化仪驱动程序,然后向该驱动程序发送报告消息,这将告诉 Windows 创建触摸事件以指明的方式。
However, starting in Windows 8, touch APIs are available in Windows to make it easier to simulate such things.
但是,从 Windows 8 开始,Windows 中提供了触摸 API,可以更轻松地模拟此类事情。

