[英]C# Move Mouse Cursor to a Specific Position on Screen for Windows 10 64bit
我试过了,但我认为它仅适用于 32 位 Windows。
我收到很多红色下划线错误,例如“游标不包含位置的定义”和“矩形不包含采用 2 个参数的构造函数”
private void MoveCursor()
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
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);
}
搜索了较旧的线程,但我认为代码已经过时并且在 Windows 64 位操作系统中不起作用。 所以我正在寻找新代码(如果有的话)。
您引用的示例是将 cursor 移动特定数量,如果您只想将鼠标移动到特定的 position,则它是过度设计的。
该代码适用于 64 位操作系统,但您需要访问 Windows Forms,但并非在 .NET 的每个版本中都可用。
在文件的顶部,您需要添加
using System.Windows.Forms;
using System.Drawing;
如果尚未包含,您还需要将对 System.Windows.Forms 的引用添加到您的项目中。 Cursor class 在System.Windows.Forms
中,Point class 在System.Drawing
中。
该代码还应该存在于表单 class 中,就像您通过 IDE 创建了一个新表单,然后向其中添加了一些代码一样。 这就是为什么它假定 Cursor 属性已经存在。
如果您只想将 cursor 移动到屏幕上的特定 position,您只需要这一行,它不需要在表单内:
Cursor.Position = new Point(x, y);
将x
和y
替换为您需要的坐标或将它们设置为int
变量。 Position 是一个 static 属性,因此您不需要先创建 Cursor 的实例。 但是 Cursor 仍然是 System.Windows.Forms 中的 class,因此您仍然需要using
它并在您的项目中引用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.