2014-09-05 75 views
3

第一次在這裏問一個問題時,我在這裏找到的解決方案似乎沒有工作出於某種原因。我的應用程序需要在窗口變爲活動狀態時設置鼠標位置,我已經設置了該功能,但無法使光標屬性起作用。出於某種原因,我無法使用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); 
+0

爲什麼你忽略錯誤信息。編譯器抱怨?閱讀錯誤消息重現它們。 – 2014-09-05 21:34:13

+0

示例:錯誤'System.Windows.Input.Cursor'不包含'Current'的定義,並且沒有找到接受'System.Windows.Input.Cursor'類型的第一個參數的擴展方法'Current' (你是否缺少using指令或程序集引用?) – DeadJohnDoe 2014-09-05 22:01:51

+0

它在System.Windows.Forms中。如文件所述。 – 2014-09-05 22:02:50

回答

3

您可以使用InteropServices輕鬆完成此操作:

// Quick and dirty sample... 
public partial class MainWindow : Window 
{ 
    [DllImport("User32.dll")] 
    private static extern bool SetCursorPos(int X, int Y); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     SetCursorPos(100, 100); 
    } 
} 

只要確保包含System.Runtime.InteropServices命名空間。還有很多其他方法,例如上面重複鏈接中指出的方法。使用最適合你的方式。

編輯:

每在評論請求,下面就來讓應用程序窗口座標系中的一種方式,而不是一個全球性:

public partial class MainWindow : Window 
{ 
    [DllImport("User32.dll")] 
    private static extern bool SetCursorPos(int X, int Y); 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     SetCursor(200, 200); 
    } 

    private static void SetCursor(int x, int y) 
    { 
     // Left boundary 
     var xL = (int)App.Current.MainWindow.Left; 
     // Top boundary 
     var yT = (int)App.Current.MainWindow.Top; 

     SetCursorPos(x + xL, y + yT); 
    } 
} 

我不認爲你會這樣做...但只要確保在初始化階段(在構造函數中)不要嘗試獲取Window座標。等到它被加載,類似於我上面所做的;否則,對於某些值,您可能會得到NaN

如果您想將其限制在窗口的限制範圍內,一個簡單的方法是將System.Windows.Forms添加到您的參考中,並使用重複鏈接中提供的代碼。但是,如果您想使用我的方法(所有關於個人偏好...我使用我喜歡的...並且我喜歡PInvoke),您可以在將它們傳遞到SetCursorPos(..)之前檢查xy職位SetCursor(..),類似於此:

private static void SetCursor(int x, int y) 
{ 
    // Left boundary 
    var xL = (int)App.Current.MainWindow.Left; 
    // Right boundary 
    var xR = xL + (int)App.Current.MainWindow.Width; 
    // Top boundary 
    var yT = (int)App.Current.MainWindow.Top; 
    // Bottom boundary 
    var yB = yT + (int)App.Current.MainWindow.Height; 

    x += xL; 
    y += yT; 

    if (x < xL) 
    { 
     x = xL; 
    } 
    else if (x > xR) 
    { 
     x = xR; 
    } 

    if (y < yT) 
    { 
     y = yT; 
    } 
    else if (y > yB) 
    { 
     y = yB; 
    } 

    SetCursorPos(x, y); 
} 

請注意,如果您的應用程序使用Windows外觀,您可能需要考慮邊框。

+0

謝謝,這個工作,我想當我嘗試類似於另一個答案,他們沒有提到所需的命名空間。我已經習慣了Eclipse中的Java,它爲它們的庫提供了自動導入功能。 – DeadJohnDoe 2014-09-05 21:37:45

+0

@DeadJohnDoe很高興幫助,只是...展開我的示例。我絕對不會那樣寫生產代碼。 – 2014-09-05 21:43:47

+0

那麼上面的函數使用的光標位置與計算機的屏幕有關而不是程序窗口?有沒有辦法使用程序窗口的座標系? – DeadJohnDoe 2014-09-05 21:58:09