WPF- 模拟触发Touch Events

简介: 原文:WPF- 模拟触发Touch Events 基于API: [DllImport("User32.dll")] public static extern bool InitializeTouchI...
原文: WPF- 模拟触发Touch Events

基于API:

  [DllImport("User32.dll")]
  public static extern bool InitializeTouchInjection(uint maxCount = 256, TouchFeedback feedbackMode = TouchFeedback.DEFAULT);


  [DllImport("User32.dll")]
  public static extern bool InjectTouchInput(int count, [MarshalAs(UnmanagedType.LPArray), In] PointerTouchInfo[] contacts);

实现效果:点击按钮,自动触发TouchDown事件、获取TouchEventArgs参数得到坐标,创建Line并设置X1、Y1属性,紧接着触发TouchMove、TouchUp事件,得到TouchUp的TouchEventArgs设置Line的X2、Y2属性。

private void MainWindow_TouchUp(object sender, TouchEventArgs e)
{
    System.Windows.Input.TouchPoint oPos = e.GetTouchPoint(this);
    this.ProxyLine.X2 = oPos.Position.X;
    this.ProxyLine.Y2 = oPos.Position.Y;
    this.GdRootZm.Children.Add(this.ProxyLine);
    Console.WriteLine("TouchID " + e.TouchDevice.Id + " TouchUp "
                       + oPos.Position.X + "    " + oPos.Position.Y);
}

private void MainWindow_TouchMove(object sender, TouchEventArgs e)
{
    System.Windows.Input.TouchPoint oPos = e.GetTouchPoint(this);
    Console.WriteLine("TouchID " + e.TouchDevice.Id + " TouchMove "
                      + oPos.Position.X + "    " + oPos.Position.Y);
}

private Line ProxyLine;

private void MainWindow_TouchDown(object sender, TouchEventArgs e)
{
    System.Windows.Input.TouchPoint oPos = e.GetTouchPoint(this);
    Line oLine = new Line();
    oLine.Stroke = new SolidColorBrush(Colors.Red);
    oLine.StrokeThickness = 2;
    oLine.X1 = oPos.Position.X;
    oLine.Y1 = oPos.Position.Y;
    this.ProxyLine = oLine;
    Console.WriteLine("TouchID " + e.TouchDevice.Id + "  TouchDown " 
                      + oPos.Position.X + "    " + oPos.Position.Y);
}

Console Write Result:

  效果图如下:

private void SimulateTouch(int x, int y)
{
    // Touch Down Simulate
    PointerTouchInfo contact = MakePointerTouchInfo(x, y, 5, 1);
    PointerFlags oFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
    contact.PointerInfo.PointerFlags = oFlags;
    bool bIsSuccess = TouchInjector.InjectTouchInput(1, new[] { contact });

    // Touch Move Simulate
    int nMoveIntervalX = this.GetRandomSeed().Next(-60, 60);
    int nMoveIntervalY = this.GetRandomSeed().Next(-60, 60);
    contact.Move(nMoveIntervalX, nMoveIntervalY);
    oFlags = PointerFlags.INRANGE | PointerFlags.INCONTACT | PointerFlags.UPDATE;
    contact.PointerInfo.PointerFlags = oFlags;
    TouchInjector.InjectTouchInput(1, new[] { contact });

    // Touch Up Simulate
    contact.PointerInfo.PointerFlags = PointerFlags.UP;
    TouchInjector.InjectTouchInput(1, new[] { contact });
}

 Source Url:https://github.com/DuelCode/TouchSimulate

 Multi Touch Also Support Like this:

private void BdrSimulateZm_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{

    // Touch Down Simulate
    int x1 = this.GetRandomSeed().Next(50, 1680 - 100);
    int y1 = this.GetRandomSeed().Next(50, 1080 - 100);
    PointerTouchInfo oContact1 = MakePointerTouchInfo(x1, y1, 5, 1);

    int x2 = this.GetRandomSeed().Next(50, 1680 - 100);
    int y2 = this.GetRandomSeed().Next(50, 1080 - 100);
    PointerTouchInfo oContact2 = MakePointerTouchInfo(x2, y2, 5, 1);

    PointerFlags oFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
    oContact1.PointerInfo.PointerFlags = oFlags;
    oContact2.PointerInfo.PointerFlags = oFlags;
    TouchInjector.InjectTouchInput(2, new[] { oContact1, oContact2 });

    // Touch Move Simulate
    int nMoveIntervalX = this.GetRandomSeed().Next(-60, 60);
    int nMoveIntervalY = this.GetRandomSeed().Next(-60, 60);
    oContact1.Move(nMoveIntervalX, nMoveIntervalY);
    oContact2.Move(nMoveIntervalX, nMoveIntervalY);
    oFlags = PointerFlags.INRANGE | PointerFlags.INCONTACT | PointerFlags.UPDATE;
    oContact1.PointerInfo.PointerFlags = oFlags;
    oContact2.PointerInfo.PointerFlags = oFlags;
    TouchInjector.InjectTouchInput(2, new[] { oContact1 , oContact2 });

    // Touch Up Simulate
    oContact1.PointerInfo.PointerFlags = PointerFlags.UP;
    oContact2.PointerInfo.PointerFlags = PointerFlags.UP;
    TouchInjector.InjectTouchInput(2, new[] { oContact1, oContact2 });
}

 

目录
相关文章
|
C#
一起谈.NET技术,WPF Multi-Touch 开发:基础触屏操作(Raw Touch)
  多点触控(Multi-Touch)就是通过与触屏设备的接触达到人与应用程序交互的操作过程。例如,生活中经常使用的触屏手机、触屏笔记本、显示器以及微软最新的Surface 产品等这些都属于触屏操作设备。
1083 0
|
C#
WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果
原文:WPF Label控件在数据绑定Content属性变化触发TargetUpdated事件简单实现类似TextChanged 事件效果   本以为Label也有TextChanged 事件,但在使用的时候却没找到,网友说Label的Content属性改变肯定是使用赋值操作,赋值的时候就可以对其进行相应的操作所以不需TextChanged 事件。
1960 0
|
C# 图形学 Windows
WPF 窗口居中 & 变更触发机制
原文:WPF 窗口居中 & 变更触发机制 窗口居中 & 变更触发机制 解决: 1。单实例窗口,窗口每次隐藏后再显示时,位置居中显示 2。多屏幕下单实例窗口,当父窗口移动到其它屏幕时,单实例窗口再次弹出时,位置才更新到父窗口屏幕。
965 0
|
C# 数据可视化 容器
WPF触屏Touch事件在嵌套控件中的响应问题
原文:WPF触屏Touch事件在嵌套控件中的响应问题 前几天遇到个touch事件的坑,记录下来以增强理解。 具体是 想把一个listview嵌套到另一个listview,这时候如果list view(子listview)的内容过多超过容器高度,它是不会出现滚动条压缩内容区域的,反而会将滚动区域转移到外面的list view(父listview),这个无可争议,但这个问题开始没留意,为待会的坑埋下伏笔。
982 0
|
C#
【WPF】代码触发Button点击事件
原文:【WPF】代码触发Button点击事件 先定义Button按钮并绑定事件。 public void test() { Button btn = new Button(); btn.
2408 0
|
C#
WPF圆角按钮与触发颜色变化
原文:WPF圆角按钮与触发颜色变化 ...
1543 0
|
C#
WPF Touch操作滚动条,Window弹跳
原文:WPF Touch操作滚动条,Window弹跳 WPF,用ScrollViewer控件,触屏开发,当滑动到最后时会使整个窗体弹跳一下 原因是因为ScrollViewer触屏操作原生支持惯性,ScrollViewer中的内容滚动到边界是会自动触发Window Bounce(窗体弹跳), 以叫做Panning Feedback(拖动回馈)。
1140 0
|
C# 图形学 Windows
WPF 窗口居中 & 变更触发机制
窗口居中 & 变更触发机制 解决: 1。单实例窗口,窗口每次隐藏后再显示时,位置居中显示 2。多屏幕下单实例窗口,当父窗口移动到其它屏幕时,单实例窗口再次弹出时,位置才更新到父窗口屏幕。 3。子窗口每次唤醒时,都居中显示。
1057 0