C#WPF应用程序.NET 4.5设置鼠标位置

问题描述:

第一次在这里问一个问题时,我在这里找到的解决方案似乎没有工作出于某种原因。我的应用程序需要在窗口变为活动状态时设置鼠标位置,我已经设置了该功能,但无法使光标属性起作用。出于某种原因,我无法使用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

您可以使用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