C# 真实地移动鼠标

标签 c# mouse

我正在演示一个软件,想构建一个鼠标“移动器”功能,这样我就可以基本上自动化该过程。我想创建逼真的鼠标移动,但在思考过程中遇到了一些心理障碍。我可以使用 C# 轻松地四处移动鼠标,但希望它比光标出现在某个 x、y 坐标然后按下按钮更真实一些。

获取鼠标当前位置,然后获取终点。计算两点之间的弧线,然后我需要计算沿该弧线的点,以便我可以向其中添加一个计时器事件,以便我可以从一个点移动到下一个点,然后重复此操作直到到达目标...

有人想详细说明吗?

谢谢,R。

最佳答案

我尝试了圆弧计算方法,结果太复杂了,最后看起来不太现实。正如 JP 在他的评论中建议的那样,直线看起来更人性化。

这是我编写的用于计算线性鼠标移动的函数。应该是不言自明的。 GetCursorPosition() 和 SetCursorPosition(Point) 是 win32 函数 GetCursorPos 和 SetCursorPos 的包装器。

就数学而言 - 从技术上讲,这称为 Linear Interpolation的线段。

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);
}

关于C# 真实地移动鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/913646/

相关文章:

javascript模拟鼠标点击特定位置

macos - 如何在Mac OSX中将焦点标签到下拉字段

bash - 我如何在 bash 中获取当前的鼠标坐标?

c# - 绑定(bind)集合不起作用

C# await udpClient.ReceiveAsync() 失败并终止程序

c# - Await 运算符只能在 Async 方法中使用

c - 如何在 linux、X 下以合法和编程方式捕获第二个鼠标或轨迹球?

c++ - 记录用于 GUI 测试的鼠标单击事件。什么比像素坐标更可靠?

c# - 将参数传递给以编程方式创建的用户控件

C# - System.Threading.Monitor 控制线程的优势