[英]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.