C#移动鼠标周围的现实
我demoing一个软件,并希望建立一个老鼠'先行者'功能,使我能够基本过程自动化。我想创建逼真的鼠标动作,但我有一点的思维过程心理障碍的。我可以很容易地左右移动鼠标用C#,但希望它不只是出现在某个X,Y坐标光标更现实一点,然后pressing一个按钮。
I am demoing a piece of software and want to build a mouse 'mover' function so that I can basically automate the process. I want to create realistic mouse movements but am having a bit of a mental block in the thought process. I can move a mouse around easily with c# but want it to be a bit more realistic than just the cursor appearing at a certain x, y, coordinates and then pressing a button.
我得到鼠标的当前位置,然后拿到终点。计算这两个点之间的弧线,但我需要计算点沿弧形,这样我可以添加一个计时器事件成这样我可以移动从一个点到下一个,然后重复此,直到我到达目标...
I get the current position of the mouse and then get the end point. Calculate an arc between the two points, but then I need to calculate points along that arc so that I can add a timer event into that so that I can move from one point to the next, and then repeat this till I get to the target...
有人要详细点吗?
谢谢你,R。
我试过弧计算方法,竟然是远远复杂,最终,它看起来并不现实。直线看起来更人性化,如JP建议在他的评论。
I tried the arc calculation method, turned out to be far to complex and, in the end, it didn't look realistic. Straight lines look much more human, as JP suggests in his comment.
这是我写的计算线性移动鼠标的功能。应该是pretty言自明。 GetCursorPosition()和SetCursorPosition(点)周围的Win32函数GetCursorPos和SetCursorPos包装。
This is a function I wrote to calculate a linear mouse movement. Should be pretty self-explanatory. GetCursorPosition() and SetCursorPosition(Point) are wrappers around the win32 functions GetCursorPos and SetCursorPos.
至于数学去 - 一个线段的技术上,这就是所谓的线性插值
As far as the math goes - technically, this is called Linear Interpolation of a line segment.
public void LinearSmoothMove(Point newPosition, int steps) {
Point start = GetCursorPosition();
PointF iterPoint = start;
// Find the slope of the line segment defined by start and newPosition
PointF slope = new PointF(newPosition.X - start.X, newPosition.Y - start.Y);
// Divide by the number of steps
slope.X = slope.X / steps;
slope.Y = slope.Y / steps;
// Move the mouse to each iterative point.
for (int i = 0; i < steps; i++)
{
iterPoint = new PointF(iterPoint.X + slope.X, iterPoint.Y + slope.Y);
SetCursorPosition(Point.Round(iterPoint));
Thread.Sleep(MouseEventDelayMS);
}
// Move the mouse to the final destination.
SetCursorPosition(newPosition);
}