C#无法将光标设置到另一个窗口

时间:2017-05-19 16:37:53

标签: c# visual-studio cursor mouse bots

我正在努力创建一个小型的自动点击器"机器人。

我需要将光标放在特定窗口中的特定位置。

使用Cursor.Position[DllImport("User32.dll", EntryPoint = "SetCursorPos")] private static extern bool SetCursorPos(int X, int Y); 无处不在,但不在那个窗口。

有什么想法吗? :)

//编辑

    [DllImport("User32.dll", EntryPoint = "SetCursorPos")]
    private static extern bool SetCursorPos(int X, int Y);

    private void OnPixelFound() {
        LblPoint.Text = "x: " + foundPixelPos.X + " | y: " + foundPixelPos.Y;
        SetCursorPos(foundPixelPos.X, foundPixelPos.Y);
    }

迎接

1 个答案:

答案 0 :(得分:0)

我假设您正在尝试在windowsforms中执行此操作?

Form2 f2 = new Form2();
    f2.Show();
    this.Focus();

完成对焦后,您可以执行鼠标移动功能。这是我的一个鼠标移动机器人的代码片段:

this.Cursor = new Cursor(Cursor.Current.Handle);
        Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
        Cursor.Clip = new Rectangle(this.Location, this.Size);

希望这是你想要完成的事情!