Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#WPF应用程序.NET 4.5设置鼠标位置_C#_Wpf_Cursor Position - Fatal编程技术网

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

C#WPF应用程序.NET 4.5设置鼠标位置,c#,wpf,cursor-position,C#,Wpf,Cursor Position,第一次在这里提问时,我在这里找到的解决方案似乎出于某种原因不起作用。当窗口处于活动状态时,我的应用程序需要设置鼠标位置,我已经设置了功能,但无法使光标属性正常工作。由于某种原因,我不能使用游标、位置或任何东西。我曾希望访问聊天室以找到解决方案,但显然,在我20岁之前,我无法发言 因此,这里我要问的是,如何使用以下内容更改光标位置 此.Cursor.SetPosition(x,y) 谢谢你的帮助 编辑:已作为测试从以下位置尝试此操作: 但是编译器会抱怨当前、位置、剪辑、位置和大小 最终解决方案:

第一次在这里提问时,我在这里找到的解决方案似乎出于某种原因不起作用。当窗口处于活动状态时,我的应用程序需要设置鼠标位置,我已经设置了功能,但无法使光标属性正常工作。由于某种原因,我不能使用游标、位置或任何东西。我曾希望访问聊天室以找到解决方案,但显然,在我20岁之前,我无法发言

因此,这里我要问的是,如何使用以下内容更改光标位置

此.Cursor.SetPosition(x,y)

谢谢你的帮助

编辑:已作为测试从以下位置尝试此操作:

但是编译器会抱怨当前、位置、剪辑、位置和大小

最终解决方案:

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);

您可以使用
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
),您可以在将
SetCursor(…)
传递到
SetCursorPos(…)
之前,检查
x
y
中的位置,类似于此:

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);
}
private static void SetCursor(int x,int y)
{
//左边界
var xL=(int)App.Current.MainWindow.Left;
//右边界
var xR=xL+(int)App.Current.MainWindow.Width;
//顶边界
var yT=(int)App.Current.MainWindow.Top;
//底边界
var yB=yT+(int)App.Current.MainWindow.Height;
x+=xL;
y+=yT;
如果(xxR)
{
x=xR;
}
if(yyB),则为else
{
y=yB;
}
SetCursorPos(x,y);
}

请注意,如果您的应用程序使用Windows look and feel,您可能需要考虑边框。

为什么忽略错误消息。编译器抱怨?读取错误消息会复制它们。例如:错误1的System.Windows.Input.Cursor不包含“Current”的定义,并且找不到接受“System.Windows.Input.Cursor”类型的第一个参数的扩展方法“Current”(是否缺少using指令或程序集引用?),它位于System.Windows.Forms中。正如文献所记载的,无论如何,websearch是有帮助的。试试这些搜索词:c#set cursor position wpfI am here from the future,意思是这个Stackoverflow现在是谷歌搜索如何在c#WPF中移动鼠标的热门搜索词。非常感谢您对Google提供了有用的评论,并没有提供答案。谢谢,这很有效,我想当我尝试与另一个答案类似的东西时,他们没有提到所需的名称空间。我已经习惯了Eclipse中的Java,它为它们的库提供了自动导入功能。@DeadJohnDoe很高兴它有帮助,只是。。。扩展我的样本。我绝对不会这样写生产代码。所以上面函数使用的光标位置与计算机屏幕相关,而不是与程序窗口相关?有没有办法改用程序窗口的坐标系?@DeadJohnDoe查看我的编辑,这样我就不必在这里输入太多。
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);
}