简体   繁体   English

C# 将鼠标 Cursor 移动到屏幕上的特定 Position Windows 10 64 位

[英]C# Move Mouse Cursor to a Specific Position on Screen for Windows 10 64bit

I tried this but I think it's for 32bit Windows only.我试过了,但我认为它仅适用于 32 位 Windows。

How to move mouse cursor using C#? 如何使用C#移动鼠标cursor?

I am getting lots of red underline errors like "Cursor does not contain a definition for Position" and "Rectangle does not contain a constructor that takes 2 arguments"我收到很多红色下划线错误,例如“游标不包含位置的定义”和“矩形不包含采用 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);
}

Searched older threads but I think the code is outdated and doesn't work in Windows 64 bit OS.搜索了较旧的线程,但我认为代码已经过时并且在 Windows 64 位操作系统中不起作用。 So I'm looking for new code if there is any.所以我正在寻找新代码(如果有的话)。

The example you are referencing is for moving the cursor by a specific amount, it is over-engineered if you just want to move the mouse to a specific position.您引用的示例是将 cursor 移动特定数量,如果您只想将鼠标移动到特定的 position,则它是过度设计的。

The code works on a 64 bit OS, but you need access to Windows Forms which is not available in every version of .NET.该代码适用于 64 位操作系统,但您需要访问 Windows Forms,但并非在 .NET 的每个版本中都可用。

At the top of the file you need to add在文件的顶部,您需要添加

using System.Windows.Forms;
using System.Drawing;

You also need to add a reference to System.Windows.Forms to your project if it is not already included.如果尚未包含,您还需要将对 System.Windows.Forms 的引用添加到您的项目中。 The Cursor class is in System.Windows.Forms and the Point class is in System.Drawing . Cursor class 在System.Windows.Forms中,Point class 在System.Drawing中。

The code is also supposed to exist inside a Form class, like if you created a new form through the IDE and then added some code to it.该代码还应该存在于表单 class 中,就像您通过 IDE 创建了一个新表单,然后向其中添加了一些代码一样。 That is why it assumes a Cursor property already exists.这就是为什么它假定 Cursor 属性已经存在。

If all you want to do is move the cursor to a specific position on screen, you only need this one line and it doesn't need to be inside a Form:如果您只想将 cursor 移动到屏幕上的特定 position,您只需要这一行,它不需要在表单内:

Cursor.Position = new Point(x, y);

Replace x and y with the coordinates you need or set them as int variables.xy替换为您需要的坐标或将它们设置为int变量。 Position is a static property so you don't need to create an instance of Cursor first. Position 是一个 static 属性,因此您不需要先创建 Cursor 的实例。 But Cursor is still a class in System.Windows.Forms so you still need to be using it and have a reference to it in your project.但是 Cursor 仍然是 System.Windows.Forms 中的 class,因此您仍然需要using它并在您的项目中引用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM