WPF:在Windows 7中是否有可能将普通鼠标事件“路由”到触摸事件?

17
我正在使用C# (.NET 4.0)和WPF为Windows 7开发触摸屏应用程序。我的问题是,我目前可用的触摸屏驱动程序只生成鼠标事件。(很遗憾,制造商没有提供真正的Windows 7驱动程序),因此,目前我无法正确测试。
有没有通用的方法来告诉Windows 7,某个设备应该是一个触摸设备(尽管这只能提供单点触控事件)?
2个回答

14

看这里。 http://blakenui.codeplex.com/。 有一个MouseTouchDevice.cs文件,它的样子像这个。它将普通鼠标事件转换为操作事件。

/// <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
}

}

我希望这是你在寻找的内容。


正是我要发布的内容,只不过你抢先一步了!干得好 :) - Hugo
如果我没有MouseButtonEventArgs该怎么办?我该如何生成触摸事件?请在这里查看我的问题:http://stackoverflow.com/questions/9716439/wpf-how-to-synthesize-a-touch-event-from-scratch - simo
如果您不想触发两次Touch promoted鼠标事件,请使用以下代码:if (e.StylusDevice != null) return; - Andreas

0
你需要重写鼠标驱动程序,使其像触摸设备一样工作才能实现这一点。一个更简单的解决方法是获取类似Wacom Bamboo Touch这样的设备吗?它是一个真正的触摸设备(不是触摸屏)。

好的,那么没有中间鼠标驱动程序这样的东西,可以配置为产生鼠标或触摸事件吗?(也许“有人”应该写一个 :)) Wacom平板电脑的想法不错,这样你就可以将设备带到不同的PC上。 - Seven
当然可以编写一个过滤驱动程序,将鼠标驱动程序包装起来并将其转换为触摸数据。但是这并不容易做到。如果你有一位已经编写了几个驱动程序的有才华的开发人员,那么可以尝试一下!当然,触摸设备会更便宜 :) - Eric Brown

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接