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 });
}

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DuelCode

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值