博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF- 模拟触发Touch Events
阅读量:6082 次
发布时间:2019-06-20

本文共 3964 字,大约阅读时间需要 13 分钟。

原文:

基于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:

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

 

你可能感兴趣的文章
数组扩展方法之求和
查看>>
astah-professional-7_2_0安装
查看>>
函数是对象-有属性有方法
查看>>
uva 10107 - What is the Median?
查看>>
Linux下基本栈溢出攻击【转】
查看>>
c# 连等算式都在做什么
查看>>
使用c:forEach 控制5个换行
查看>>
java web轻量级开发面试教程摘录,java web面试技巧汇总,如何准备Spring MVC方面的面试...
查看>>
使用ansible工具部署ceph
查看>>
linux系列博文---->深入理解linux启动运行原理(一)
查看>>
Android反编译(一) 之反编译JAVA源码
查看>>
结合当前公司发展情况,技术团队情况,设计一个适合的技术团队绩效考核机制...
查看>>
python-45: opener 的使用
查看>>
cad图纸转换完成的pdf格式模糊应该如何操作?
查看>>
Struts2与Struts1区别
查看>>
网站内容禁止复制解决办法
查看>>
Qt多线程
查看>>
我的友情链接
查看>>
想说一点东西。。。。
查看>>
css知多少(8)——float上篇
查看>>