如何在不使用触摸屏的情况下模拟触摸事件

我目前正在尝试开发一个触摸屏应用程序:

  • Windows 7的
  • Visual Studio 2013
  • C# – WPF

我正在工作的地方将会收到一个触摸屏(实际上是一个放在平面屏幕上的图层)。

我希望能够生成触摸输入,以便在没有屏幕的情况下开发和测试应用程序。

我发现的所有资源要么很旧,要么很复杂。

如今,在没有触摸屏的情况下开发和测试触摸屏应用的最佳方式是什么?

一种方法是将第二个鼠标连接到工作站。 然后你可以测试多点触控(resize,旋转等)。 几年前我成功地做到了。 你需要适当的驱动程序。 请检查moultitouch项目 。 我想我正在使用它或类似的东西。

您可以在此超级用户post中找到更多建议。 但我从没试过它们。

编辑

检查您的评论后,我更了解您的问题。 试试这个StackOverflow线程 。 它正在讨论将鼠标重新路由到触摸事件中。 还检查Blake.NUI项目 – 它改进了WPF 4以更好地处理触摸交互(以及其他事项)。

在项目中,您将找到MouseTouchDevice类,它可以帮助您将鼠标转换为触摸事件:

  ///  /// Used to translate mouse events into touch events, enabling a unified /// input processing pipeline. ///  /// This class originally comes from Blake.NUI - http://blakenui.codeplex.com 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 } 

我在win 8的开发者预览中使用了它。它支持单点触摸,缩放手势和旋转手势。

我知道如何在Windows 7中执行此操作(不会被视为“黑客”)的唯一方法是创建一个HID数字化仪驱动程序,然后向该驱动程序发送报告消息,这将告诉Windows创建触摸事件以指定的方式。

但是,从Windows 8开始,Windows中提供触摸API,以便更轻松地模拟此类内容。