123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Uni_Trend.MSO7000X.Touch
- {
- /// <summary>
- /// 触摸功能的帮助器
- /// </summary>
- public class TouchHelper
- {
- readonly Control _TouchControl; //帮助器绑定的控件对象
- IntPtr _ManipulationHelper = IntPtr.Zero; //操作帮助器指针
- bool _TouchingFlag = false; //表示控件对象是否正处于触摸状态
- //缩放功能相关变量
- ScaleDirection _SclDrection = ScaleDirection.Both;
- (int xSum, int ySum) _CurrentTouchPointSum = (0, 0);
- (int xSum, int ySum) _LastScaleTouchPointSum = (0, 0);
- //触摸回调函数对应的委托
- TouchWin32.OnManipulationStartedDelegate _OnManipulationStartedDelegate;
- TouchWin32.OnManipulationDeltaDelegate _OnManipulationDeltaDelegate;
- TouchWin32.OnManipulationCompletedDelegate _OnManipulationCompletedDelegate;
- //表示控件对象是否正处于触摸状态
- public bool TouchingFlag => _TouchingFlag;
- /// <summary>
- /// 参数1:触点的X坐标;
- /// 参数2:触点的Y坐标;
- /// </summary>
- public event Action<int, int> TouchDown;
- /// <summary>
- /// 参数1:触点当前的X坐标;
- /// 参数2:触点当前的Y坐标;
- /// 参数3:触点此刻移动的X距离;
- /// 参数4:触点此刻移动的Y距离;
- /// </summary>
- public event Action<int, int, int, int> TouchMove;
- /// <summary>
- /// 参数1:触点的X坐标;
- /// 参数2:触点的Y坐标;
- /// </summary>
- public event Action<int, int> TouchUp;
- /// <summary>
- /// 参数:旋转的角度;
- /// </summary>
- public event Action<float> TouchRotation;
- /// <summary>
- /// 参数:缩放的倍率;
- /// </summary>
- public event Action<float, ScaleDirection> TouchScale;
- public TouchHelper(Control touchCtl)
- {
- _TouchControl = touchCtl;
- RegisterTouch();
- }
- /// <summary>
- /// 用该方法来解析Touch消息,用在WndProc里面使用;
- /// </summary>
- /// <param name="touchMsg"></param>
- public void DecodeTouch(ref Message touchMsg)
- {
- if(_ManipulationHelper == IntPtr.Zero)
- {
- return;
- }
- int cinputs = (int)touchMsg.WParam; // Number of actual per-contact messages
- TouchWin32.TOUCHINPUT[] pinputs = new TouchWin32.TOUCHINPUT[cinputs]; // Allocate the storage for the parameters of the per-contact messages
- // Unpack message parameters into the array of TOUCHINPUT structures, each
- // representing a message for one single contact.
- if (TouchWin32.GetTouchInputInfo(touchMsg.LParam, cinputs, pinputs, Marshal.SizeOf(typeof(TouchWin32.TOUCHINPUT))))
- {
- _CurrentTouchPointSum.xSum = 0;
- _CurrentTouchPointSum.ySum = 0;
- pinputs.Cast<TouchWin32.TOUCHINPUT>().ToList().ForEach(input =>
- {
- _CurrentTouchPointSum.xSum += input.x;
- _CurrentTouchPointSum.ySum += input.y;
- });
- // For each contact, dispatch the message to the appropriate message handler.
- for (uint i = 0; i < cinputs; i++)
- {
- if ((pinputs[i].dwFlags & TouchWin32.TOUCHEVENTF_DOWN) != 0)
- {
- TouchWin32.ProcessDown(_ManipulationHelper, pinputs[i].dwID, pinputs[i].x, pinputs[i].y);
- }
- else if ((pinputs[i].dwFlags & TouchWin32.TOUCHEVENTF_MOVE) != 0)
- {
- TouchWin32.ProcessMove(_ManipulationHelper, pinputs[i].dwID, pinputs[i].x, pinputs[i].y);
- }
- else if ((pinputs[i].dwFlags & TouchWin32.TOUCHEVENTF_UP) != 0)
- {
- TouchWin32.ProcessUp(_ManipulationHelper, pinputs[i].dwID, pinputs[i].x, pinputs[i].y);
- }
- }
- }
- TouchWin32.CloseTouchInputHandle(touchMsg.LParam);
- }
- /// <summary>
- /// 处理触摸开始事件的委托函数;
- /// 注意:委托直接由IManipulationProcessor相关线程调用,由于该线程不能耗时,所以委托函数要用多线程处理;
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- protected virtual async void OnManipulationStarted(
- float x,
- float y)
- {
- await Task.Run(() =>
- {
- Point clientpoint = TranslateTouchXYToClient(x, y);
- //触发TouchDown事件
- if (TouchDown != null)
- {
- _TouchControl.Invoke(TouchDown, clientpoint.X, clientpoint.Y);
- }
- });
- _TouchingFlag = true;
- }
- /// <summary>
- /// 处理触摸变化事件的委托函数;
- /// 注意:委托直接由IManipulationProcessor相关线程调用,由于该线程不能耗时,所以委托函数要用多线程处理;
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <param name="translationDeltaX"></param>
- /// <param name="translationDeltaY"></param>
- /// <param name="scaleDelta"></param>
- /// <param name="expansionDelta"></param>
- /// <param name="rotationDelta"></param>
- /// <param name="cumulativeTranslationX"></param>
- /// <param name="cumulativeTranslationY"></param>
- /// <param name="cumulativeScale"></param>
- /// <param name="cumulativeExpansion"></param>
- /// <param name="cumulativeRotation"></param>
- protected virtual void OnManipulationDelta(
- float x,
- float y,
- float translationDeltaX,
- float translationDeltaY,
- float scaleDelta,
- float expansionDelta,
- float rotationDelta,
- float cumulativeTranslationX,
- float cumulativeTranslationY,
- float cumulativeScale,
- float cumulativeExpansion,
- float cumulativeRotation)
- {
- //不在触摸状态,不用处理
- if(!_TouchingFlag)
- {
- return;
- }
- //触发相关事件
- Task.Run(() =>
- {
- Point clientpoint = TranslateTouchXYToClient(x, y);
- //触发TouchRotation事件
- if (TouchRotation != null && rotationDelta != 0)
- {
- _TouchControl.Invoke(TouchRotation, rotationDelta);
- }
- //触发TouchScale事件,当此事件发生事,TouchMove事件不发生;
- if (TouchScale != null && scaleDelta != 1F)
- {
- _SclDrection = CalculateDirctByXYSum();
- _TouchControl.Invoke(TouchScale, scaleDelta, _SclDrection);
- return;
- }
- //触发TouchMove事件
- if (TouchMove != null)
- {
- if (translationDeltaX != 0 || translationDeltaY != 0)
- {
- _TouchControl.Invoke(
- TouchMove,
- clientpoint.X,
- clientpoint.Y,
- (int)(translationDeltaX / 100),
- (int)(translationDeltaY / 100));
- }
- }
- });
- }
- /// <summary>
- /// 处理触摸完成事件的委托函数;
- /// 注意:委托直接由IManipulationProcessor相关线程调用,由于该线程不能耗时,所以委托函数要用多线程处理;
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <param name="cumulativeTranslationX"></param>
- /// <param name="cumulativeTranslationY"></param>
- /// <param name="cumulativeScale"></param>
- /// <param name="cumulativeExpansion"></param>
- /// <param name="cumulativeRotation"></param>
- protected virtual async void OnManipulationCompleted(
- float x,
- float y,
- float cumulativeTranslationX,
- float cumulativeTranslationY,
- float cumulativeScale,
- float cumulativeExpansion,
- float cumulativeRotation)
- {
- await Task.Run(() =>
- {
- Point clientpoint = TranslateTouchXYToClient(x, y);
- //触发TouchUp事件
- if (TouchUp != null)
- {
- _TouchControl.Invoke(TouchUp, clientpoint.X, clientpoint.Y);
- }
- });
- _TouchingFlag = false;
- }
- /// <summary>
- /// 注册触摸事件到控件
- /// </summary>
- /// <param name="ctlHandle">控件句柄</param>
- private void RegisterTouch()
- {
- _OnManipulationStartedDelegate = new TouchWin32.OnManipulationStartedDelegate(OnManipulationStarted);
- _OnManipulationDeltaDelegate = new TouchWin32.OnManipulationDeltaDelegate(OnManipulationDelta);
- _OnManipulationCompletedDelegate = new TouchWin32.OnManipulationCompletedDelegate(OnManipulationCompleted);
- _ManipulationHelper = TouchWin32.RegisterWnd(
- _TouchControl.Handle,
- Marshal.GetFunctionPointerForDelegate(_OnManipulationStartedDelegate),
- Marshal.GetFunctionPointerForDelegate(_OnManipulationDeltaDelegate),
- Marshal.GetFunctionPointerForDelegate(_OnManipulationCompletedDelegate));
- }
- /// <summary>
- /// 转换touch信息中的x和y坐标到绑定控件(_TouchControl)的坐标系
- /// </summary>
- /// <param name="touchX">相对与屏幕坐标系的X坐标,且精度为Pixel的1/100</param>
- /// <param name="touchY">相对与屏幕坐标系的Y坐标,且精度为Pixel的1/100</param>
- private Point TranslateTouchXYToClient(float touchX, float touchY)
- {
- Point clientpoint;
- Point screenpoint = new Point((int)(touchX / 100), (int)(touchY / 100));
- if(_TouchControl.InvokeRequired)
- {
- clientpoint = (Point)_TouchControl.Invoke(
- new Func<Point>(() => _TouchControl.PointToClient(screenpoint)));
- }
- else
- {
- clientpoint = _TouchControl.PointToClient(screenpoint);
- }
- return clientpoint;
- }
- /// <summary>
- /// 计算缩放方向的方法:通过XY的移动量来确定
- /// </summary>
- /// <param name="xTrans"></param>
- /// <param name="yTrans"></param>
- /// <returns></returns>
- private ScaleDirection CalculateDirctByXY(float xTrans, float yTrans)
- {
- if (Math.Abs(xTrans) > Math.Abs(yTrans))
- {
- return ScaleDirection.Horizontal;
- }
- else if (Math.Abs(xTrans) == Math.Abs(yTrans))
- {
- return ScaleDirection.Both;
- }
- else
- {
- return ScaleDirection.Veritcal;
- }
- }
- /// <summary>
- /// 计算缩放方向的方法:通过X,Y的和的变化量来确定
- /// </summary>
- /// <returns></returns>
- private ScaleDirection CalculateDirctByXYSum()
- {
- int xdelta = Math.Abs(_CurrentTouchPointSum.xSum - _LastScaleTouchPointSum.xSum);
- int ydelta = Math.Abs(_CurrentTouchPointSum.ySum - _LastScaleTouchPointSum.ySum);
- _LastScaleTouchPointSum.xSum = _CurrentTouchPointSum.xSum;
- _LastScaleTouchPointSum.ySum = _CurrentTouchPointSum.ySum;
-
- if (xdelta > ydelta)
- {
- return ScaleDirection.Horizontal;
- }
- else if(xdelta < ydelta)
- {
- return ScaleDirection.Veritcal;
- }
- return ScaleDirection.Both;
- }
- }
- /// <summary>
- /// 缩放的方向定义
- /// </summary>
- public enum ScaleDirection
- {
- Both,
- Horizontal,
- Veritcal
- }
- }
|