/// <summary> /// Moves the mouse by a given delta from the current position. /// </summary> /// <param name="newPosition">The delta for the x-axis.</param> /// <param name="duration">The time to interpolate the move.</param> public static void MoveTo(Point newPosition, TimeSpan duration) { Wait.UntilInputIsProcessed(); if (duration.Ticks == 0) { if (!User32.SetCursorPos((int)newPosition.X, (int)newPosition.Y)) { throw new Win32Exception(); } } else { var interpolation = Interpolation.Start(Position, newPosition, duration); while (interpolation.TryGetPosition(out var pos)) { if (!User32.SetCursorPos(pos.X, pos.Y)) { throw new Win32Exception(); } Wait.For(TimeSpan.FromMilliseconds(20)); } } Wait.UntilInputIsProcessed(); }
/// <summary> /// Simulate touch drag. /// Call <see cref="Hold(Point)"/> before calling this method. /// This method is useful when dragging to multiple positions. /// </summary> /// <param name="position">The position.</param> public static void DragTo(Point position) { if (contacts is null || contacts.Length != 1) { throw new UiAutomationException("Call Touch.Down first."); } var interpolation = Interpolation.Start(contacts[0].PointerInfo.PtPixelLocation, POINT.From(position), MoveSpeed); while (interpolation.TryGetPosition(out var pos)) { contacts[0].PointerInfo.PointerFlags = POINTER_FLAG.UPDATE | POINTER_FLAG.INRANGE | POINTER_FLAG.INCONTACT; contacts[0].PointerInfo.PtPixelLocation = pos; if (!User32.InjectTouchInput(1, contacts)) { throw new Win32Exception(); } Wait.For(TimeSpan.FromMilliseconds(10)); } }
/// <summary> /// Drags the mouse in one step. /// </summary> /// <param name="mouseButton">The mouse button to use for dragging.</param> /// <param name="from">Start point of the drag.</param> /// <param name="to">End point for the drga.</param> /// <param name="duration">The time to perform the drag.</param> public static void Drag(MouseButton mouseButton, Point from, Point to, TimeSpan duration) { if (duration.Ticks == 0) { if (!User32.SetCursorPos((int)from.X, (int)from.Y)) { throw new Win32Exception(); } using (Hold(mouseButton)) { if (!User32.SetCursorPos((int)to.X, (int)to.Y)) { throw new Win32Exception(); } } } else { Position = from; using (Hold(mouseButton)) { var interpolation = Interpolation.Start(from, to, duration); while (interpolation.TryGetPosition(out var pos)) { if (!User32.SetCursorPos(pos.X, pos.Y)) { throw new Win32Exception(); } Wait.For(TimeSpan.FromMilliseconds(10)); } } } Wait.UntilInputIsProcessed(); }