wpf 触摸命令 注入(模拟触屏操作)
Posted chlm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf 触摸命令 注入(模拟触屏操作)相关的知识,希望对你有一定的参考价值。
使用TCD.System.TouchInjection.dll
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using TCD.System.TouchInjection; namespace WpfApp1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); TouchInjector.InitializeTouchInjection(); } StringBuilder data = new StringBuilder(); private void Image_TouchDown(object sender, TouchEventArgs e) { var pos = e.GetTouchPoint(this); var screenPos = PointToScreen(new Point(pos.Position.X, pos.Position.Y)); var x = (int)screenPos.X; var y = (int)screenPos.Y; data.Append(e.TouchDevice.Id + ",down," + x + "," + y + ";"); } private void Image_TouchUp(object sender, TouchEventArgs e) { var pos = e.GetTouchPoint(this); var screenPos = PointToScreen(new Point(pos.Position.X, pos.Position.Y)); var x = (int)screenPos.X; var y = (int)screenPos.Y; data.Append(e.TouchDevice.Id + ",up," + x + "," + y + ";"); } private async void redo_Click(object sender, RoutedEventArgs e) { //求出起点,终点 var dataStr = data.ToString(); var list = dataStr.Split(‘;‘); List<Tuple<int, string, int, int>> structedList = new List<Tuple<int, string, int, int>>(); foreach (var item in list) { var info = item.Split(‘,‘); if (info.Length < 3) { continue; } var id = int.Parse(info[0]); var type = info[1]; var x = int.Parse(info[2]); var y = int.Parse(info[3]); structedList.Add(Tuple.Create(id, type, x, y)); } List<Tuple<int, int, int, int>> device = new List<Tuple<int, int, int, int>>(); if (structedList.Count > 1) { var idGroup = structedList.GroupBy(x => x.Item1); foreach (var item in idGroup) { int fromX = 0, fromY = 0, toX = 0, toY = 0; foreach (var child in item) { if (child.Item2 == "down") { fromX = child.Item3; fromY = child.Item4; } if (child.Item2 == "up") { toX = child.Item3; toY = child.Item4; } } device.Add(Tuple.Create(fromX, fromY, toX, toY)); } if (device.Count > 1) { Task.Factory.StartNew(() => { SimulatePinch(device[0].Item1, device[0].Item2, device[0].Item3, device[0].Item4, device[1].Item1, device[1].Item2, device[1].Item3, device[1].Item4); }); } } } private async Task SimulatePinch(int fromX, int fromY, int toX, int toY, int fromX2, int fromY2, int toX2, int toY2) { // await Task.Delay(2000);//wait for two seconds, so the user can switch windows // PointerTouchInfo[] contacts = new PointerTouchInfo[2]; contacts[0] = MakePointerTouchInfo(fromX, fromY, 2, 1); contacts[1] = MakePointerTouchInfo(fromX2, fromY2, 2, 2); bool success = TouchInjector.InjectTouchInput(2, contacts); Thread.Sleep(2); contacts[0].PointerInfo.PointerFlags = PointerFlags.UPDATE | PointerFlags.INRANGE | PointerFlags.INCONTACT; contacts[1].PointerInfo.PointerFlags = PointerFlags.UPDATE | PointerFlags.INRANGE | PointerFlags.INCONTACT; //drag them from/to each other for (int i = 0; i < 100; i++) { contacts[0].Move((toX - fromX) / 100, (toY - fromY) / 100); contacts[1].Move((toX2 - fromX2) / 100, (toY2 - fromY2) / 100); bool s = TouchInjector.InjectTouchInput(2, contacts); Thread.Sleep(2); } //release them contacts[0].PointerInfo.PointerFlags = PointerFlags.UP; contacts[1].PointerInfo.PointerFlags = PointerFlags.UP; bool success2 = TouchInjector.InjectTouchInput(2, contacts); } private PointerTouchInfo MakePointerTouchInfo(int x, int y, int radius, uint id, uint orientation = 90, uint pressure = 32000) { PointerTouchInfo contact = new PointerTouchInfo(); contact.PointerInfo.pointerType = PointerInputType.TOUCH; contact.TouchFlags = TouchFlags.NONE; contact.Orientation = orientation; contact.Pressure = pressure; contact.PointerInfo.PointerFlags = PointerFlags.DOWN | PointerFlags.INRANGE | PointerFlags.INCONTACT; contact.TouchMasks = TouchMask.CONTACTAREA | TouchMask.ORIENTATION | TouchMask.PRESSURE; contact.PointerInfo.PtPixelLocation.X = x; contact.PointerInfo.PtPixelLocation.Y = y; contact.PointerInfo.PointerId = id; contact.ContactArea.left = x - radius; contact.ContactArea.right = x + radius; contact.ContactArea.top = y - radius; contact.ContactArea.bottom = y + radius; return contact; } } }
以上是关于wpf 触摸命令 注入(模拟触屏操作)的主要内容,如果未能解决你的问题,请参考以下文章