码迷,mamicode.com
首页 > Windows程序 > 详细

wpf 触摸命令 注入(模拟触屏操作)

时间:2020-04-18 15:45:35      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:ted   generic   触摸   i++   起点   sim   tac   move   inpu   

使用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 触摸命令 注入(模拟触屏操作)

标签:ted   generic   触摸   i++   起点   sim   tac   move   inpu   

原文地址:https://www.cnblogs.com/chlm/p/12726084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!