/// <summary> /// Create new gesture event instance and decode the gesture info structure /// </summary> /// <param name="handler">The gesture handler</param> /// <param name="gestureInfo">The gesture information</param> internal GestureEventArgs(GestureHandler handler, ref GESTUREINFO gestureInfo) { _dwFlags = gestureInfo.dwFlags; GestureId = gestureInfo.dwID; GestureArguments = gestureInfo.ullArguments; //Get the last event from the handler LastEvent = handler.LastEvent; //Get the last begin event from the handler LastBeginEvent = handler.LastBeginEvent; DecodeGesture(handler.HWndWrapper, ref gestureInfo); //new gesture, clear last and first event fields if (IsBegin) { LastBeginEvent = null; LastEvent = null; } }
public static extern bool GetGestureInfo(IntPtr hGestureInfo, ref GESTUREINFO pGestureInfo);
/// <summary> /// The Windows message interception for gesture events handling /// </summary> /// <param name="hWnd">WndProc hWnd</param> /// <param name="msg">WndProc msg</param> /// <param name="wParam">WndProc wParam</param> /// <param name="lParam">WndProc lParam</param> /// <returns>WndProc return</returns> public override uint WindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam) { //We care only for gesture events if (msg != User32.WM_GESTURE) return 0; GESTUREINFO gestureInfo = new GESTUREINFO { cbSize = (uint)Marshal.SizeOf(typeof(GESTUREINFO)) }; bool result = User32.GetGestureInfo(lParam, ref gestureInfo); if (!result) throw new Exception("Cannot get gesture information"); //Decode the gesture info and get the message event argument GestureEventArgs eventArgs = new GestureEventArgs(this, ref gestureInfo); try { //Fire the event using the event map _eventMap[MapWM2EventId(gestureInfo.dwID, gestureInfo.dwFlags)].Invoke(this, eventArgs); } catch (ArgumentOutOfRangeException) //In case future releases will introduce new event values { } //Keep the last message for relative calculations LastEvent = eventArgs; //Keep the first message for relative calculations if (eventArgs.IsBegin) LastBeginEvent = eventArgs; User32.CloseGestureInfoHandle(lParam); return 1; }
//Decode the gesture private void DecodeGesture(IHwndWrapper hWndWrapper, ref GESTUREINFO gestureInfo) { Location = hWndWrapper.PointToClient(new Point(gestureInfo.ptsLocation.x, gestureInfo.ptsLocation.y)); Center = Location; switch (GestureId) { case User32.GID_ROTATE: ushort lastArguments = (ushort)(IsBegin ? 0 : LastEvent.GestureArguments); RotateAngle = User32.GID_ROTATE_ANGLE_FROM_ARGUMENT((ushort)(gestureInfo.ullArguments - lastArguments)); break; case User32.GID_ZOOM: Point first = IsBegin ? Location : LastBeginEvent.Location; Center = new Point((Location.X + first.X) / 2, (Location.Y + first.Y) / 2); ZoomFactor = IsBegin ? 1 : (double)gestureInfo.ullArguments / LastEvent.GestureArguments; //DistanceBetweenFingers = User32.LoDWord(gestureInfo.ullArguments); break; case User32.GID_PAN: PanTranslation = IsBegin ? new Size(0, 0) : new Size(Location.X - LastEvent.Location.X, Location.Y - LastEvent.Location.Y); int panVelocity = User32.HiDWord((long)(gestureInfo.ullArguments)); PanVelocity = new Size(User32.LoWord(panVelocity), User32.HiWord(panVelocity)); //DistanceBetweenFingers = User32.LoDWord(gestureInfo.ullArguments); break; } }