原文:WPF- 模拟触发Touch Events

基于API:

  1. [DllImport("User32.dll")]
  2. public static extern bool InitializeTouchInjection(uint maxCount = 256, TouchFeedback feedbackMode = TouchFeedback.DEFAULT);
  3. [DllImport("User32.dll")]
  4. 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属性。

  1. private void MainWindow_TouchUp(object sender, TouchEventArgs e)
  2. {
  3. System.Windows.Input.TouchPoint oPos = e.GetTouchPoint(this);
  4. this.ProxyLine.X2 = oPos.Position.X;
  5. this.ProxyLine.Y2 = oPos.Position.Y;
  6. this.GdRootZm.Children.Add(this.ProxyLine);
  7. Console.WriteLine("TouchID " + e.TouchDevice.Id + " TouchUp "
  8. + oPos.Position.X + " " + oPos.Position.Y);
  9. }
  10. private void MainWindow_TouchMove(object sender, TouchEventArgs e)
  11. {
  12. System.Windows.Input.TouchPoint oPos = e.GetTouchPoint(this);
  13. Console.WriteLine("TouchID " + e.TouchDevice.Id + " TouchMove "
  14. + oPos.Position.X + " " + oPos.Position.Y);
  15. }
  16. private Line ProxyLine;
  17. private void MainWindow_TouchDown(object sender, TouchEventArgs e)
  18. {
  19. System.Windows.Input.TouchPoint oPos = e.GetTouchPoint(this);
  20. Line oLine = new Line();
  21. oLine.Stroke = new SolidColorBrush(Colors.Red);
  22. oLine.StrokeThickness = 2;
  23. oLine.X1 = oPos.Position.X;
  24. oLine.Y1 = oPos.Position.Y;
  25. this.ProxyLine = oLine;
  26. Console.WriteLine("TouchID " + e.TouchDevice.Id + " TouchDown "
  27. + oPos.Position.X + " " + oPos.Position.Y);
  28. }

Console Write Result:

  效果图如下:

  1. private void SimulateTouch(int x, int y)
  2. {
  3. // Touch Down Simulate
  4. PointerTouchInfo contact = MakePointerTouchInfo(x, y, 5, 1);
  5. PointerFlags oFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
  6. contact.PointerInfo.PointerFlags = oFlags;
  7. bool bIsSuccess = TouchInjector.InjectTouchInput(1, new[] { contact });
  8. // Touch Move Simulate
  9. int nMoveIntervalX = this.GetRandomSeed().Next(-60, 60);
  10. int nMoveIntervalY = this.GetRandomSeed().Next(-60, 60);
  11. contact.Move(nMoveIntervalX, nMoveIntervalY);
  12. oFlags = PointerFlags.INRANGE | PointerFlags.INCONTACT | PointerFlags.UPDATE;
  13. contact.PointerInfo.PointerFlags = oFlags;
  14. TouchInjector.InjectTouchInput(1, new[] { contact });
  15. // Touch Up Simulate
  16. contact.PointerInfo.PointerFlags = PointerFlags.UP;
  17. TouchInjector.InjectTouchInput(1, new[] { contact });
  18. }

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

 Multi Touch Also Support Like this:

  1. private void BdrSimulateZm_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  2. {
  3. // Touch Down Simulate
  4. int x1 = this.GetRandomSeed().Next(50, 1680 - 100);
  5. int y1 = this.GetRandomSeed().Next(50, 1080 - 100);
  6. PointerTouchInfo oContact1 = MakePointerTouchInfo(x1, y1, 5, 1);
  7. int x2 = this.GetRandomSeed().Next(50, 1680 - 100);
  8. int y2 = this.GetRandomSeed().Next(50, 1080 - 100);
  9. PointerTouchInfo oContact2 = MakePointerTouchInfo(x2, y2, 5, 1);
  10. PointerFlags oFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT;
  11. oContact1.PointerInfo.PointerFlags = oFlags;
  12. oContact2.PointerInfo.PointerFlags = oFlags;
  13. TouchInjector.InjectTouchInput(2, new[] { oContact1, oContact2 });
  14. // Touch Move Simulate
  15. int nMoveIntervalX = this.GetRandomSeed().Next(-60, 60);
  16. int nMoveIntervalY = this.GetRandomSeed().Next(-60, 60);
  17. oContact1.Move(nMoveIntervalX, nMoveIntervalY);
  18. oContact2.Move(nMoveIntervalX, nMoveIntervalY);
  19. oFlags = PointerFlags.INRANGE | PointerFlags.INCONTACT | PointerFlags.UPDATE;
  20. oContact1.PointerInfo.PointerFlags = oFlags;
  21. oContact2.PointerInfo.PointerFlags = oFlags;
  22. TouchInjector.InjectTouchInput(2, new[] { oContact1 , oContact2 });
  23. // Touch Up Simulate
  24. oContact1.PointerInfo.PointerFlags = PointerFlags.UP;
  25. oContact2.PointerInfo.PointerFlags = PointerFlags.UP;
  26. TouchInjector.InjectTouchInput(2, new[] { oContact1, oContact2 });
  27. }

 

WPF- 模拟触发Touch Events的更多相关文章

  1. wpf 模拟3D效果(和手机浏览图片效果相似)(附源码)

    原文 wpf 模拟3D效果(和手机浏览图片效果相似)(附源码) pf的3D是一个很有意思的东西,类似于ps的效果,类似于电影动画的效果,因为动画的效果,(对于3D基础的摄像机,光源,之类不介绍,对于依 ...

  2. iOS Programming Touch Events and UIResponder

    iOS Programming Touch Events and UIResponder  1 Touch Events  As a subclass of UIResponder, a UIView ...

  3. js模拟触发事件

     html标签元素封装着实用的[事件],但在很多时候,需要[模拟触发事件],比如 [按钮单机事件]  可以实实在在点击按钮触发该事件,但体验而言,很多时候需要js逻辑处理让实现 触发事件的效果这时就用 ...

  4. [React Native] State and Touch Events -- TextInput, TouchableHighLight

    In React, components manage their own state. In this lesson, we'll walk through building a component ...

  5. WPF 模拟UI 键盘录入

    原文:WPF 模拟UI 键盘录入 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/article/details/1835 ...

  6. WPF模拟探照灯文字

    原文:WPF模拟探照灯文字 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/article/details/1835936 ...

  7. WPF模拟雷达界面效果图

    原文:WPF模拟雷达界面效果图 iPad塔防的防守兵的效果很炫,2个小时用WPF模拟了一个. 效果图: 关键代码: <Grid> <Grid.Background> <I ...

  8. WPF模拟Office2010文件菜单的TabControl模板

    原文:WPF模拟Office2010文件菜单的TabControl模板 这是Office2010中的文件菜单点开后的效果.本文我将以强大的WPF(www.itstrike.cn)来实现类似的效果.希望 ...

  9. wpf 模拟抖音很火的罗盘时钟,附源码,下载就能跑

    wpf 模拟抖音很火的罗盘时钟,附源码 前端时间突然发现,抖音火了个壁纸,就是黑底蕾丝~~~  错错错,黑底白字的罗盘时钟! 作为程序员的我,也觉得很新颖,所以想空了研究下,这不,空下来了就用wpf, ...

随机推荐

  1. css3-6 表格如何设置样式和定位样式是什么

    css3-6 表格如何设置样式和定位样式是什么 一.总结 一句话总结:css可以解决所有属性设置的样式. 1.表格如何设置样式? css样式可以解决一切问题,没必要在表格上面加属性来设置样式. 7 t ...

  2. Redis主从高可用缓存

    nopCommerce 3.9 大波浪系列 之 使用Redis主从高可用缓存   一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集 ...

  3. php解析xml字符串

    <?php $content = <<<XML <?xml version="1.0" encoding="UTF-8"?> ...

  4. thinkphp mysql 坐标按距离排序

    $cha1 = new Model(); $shops = $cha1->query("select *,(2 * 6378.137* ASIN(SQRT(POW(SIN(3.1415 ...

  5. Mysql 安装(Using Generic Binaries)

    本次 Mysql 为Community 5.6.21 版本号.安装方式为通用Linux安装方式.即大多数Linux平台都能够採用该方式进行安装. 一.安装步骤 1.安装环境 1)Centos 7.0. ...

  6. Uncaught SyntaxError: Invalid regular expression flags(看页面源代码)

    Uncaught SyntaxError: Invalid regular expression flags(看页面源代码) 一.总结 js或者jquery方面的错误看页面源代码,一下子错误就很清晰了 ...

  7. Android 设置图片 Bitmap任意透明度

    两种思路,第一种思路是通过对Bitmap进行操作,将Bitmap的像素值get到一个int[]数组里,因为在android里Bitmap通常是ARGB8888格式,所以最高位就是A通道的值,对齐进行改 ...

  8. 1069. The Black Hole of Numbers (20)【模拟】——PAT (Advanced Level) Practise

    题目信息 1069. The Black Hole of Numbers (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B For any 4-digit inte ...

  9. 【codeforces 604D】Moodular Arithmetic

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  10. 【codeforces 765A】Neverending competitions

    [题目链接]:http://codeforces.com/contest/765/problem/A [题意] 给你一个人的n个行程 行程都是从家到某个地方或从某个地方到家; 且是无序的,且如果到了非 ...