第一次在這裏問一個問題時,我在這裏找到的解決方案似乎沒有工作出於某種原因。我的應用程序需要在窗口變爲活動狀態時設置鼠標位置,我已經設置了該功能,但無法使光標屬性起作用。出於某種原因,我無法使用Cursor.Position或其他任何東西。我曾希望訪問聊天室尋找解決方案,但顯然我不能說話,直到我有20名聲望。C#WPF應用程序.NET 4.5設置鼠標位置
所以在這裏我問如何改變光標位置的東西,如
this.Cursor.SetPosition(X,Y);
感謝您的幫助。
編輯:嘗試這已作爲測試從here:
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);
}
但編譯器抱怨當前,位置,夾,位置,大小
最終的解決方案:
using System.Runtime.InteropServices;
...
[DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);
...
Point relativePoint = MouseCaptureButton.TransformToAncestor(this)
.Transform(new Point(0, 0));
Point pt = new Point(relativePoint.X + MouseCaptureButton.ActualWidth/2,
relativePoint.Y + MouseCaptureButton.ActualHeight/2);
Point windowCenterPoint = pt;//new Point(125, 80);
Point centerPointRelativeToSCreen = this.PointToScreen(windowCenterPoint);
SetCursorPos((int)centerPointRelativeToSCreen.X, (int)centerPointRelativeToSCreen.Y);
爲什麼你忽略錯誤信息。編譯器抱怨?閱讀錯誤消息重現它們。 – 2014-09-05 21:34:13
示例:錯誤'System.Windows.Input.Cursor'不包含'Current'的定義,並且沒有找到接受'System.Windows.Input.Cursor'類型的第一個參數的擴展方法'Current' (你是否缺少using指令或程序集引用?) – DeadJohnDoe 2014-09-05 22:01:51
它在System.Windows.Forms中。如文件所述。 – 2014-09-05 22:02:50