ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-09 21:52:59  阅读:243  来源: 互联网

标签:c wpf cursor-position


参见英文答案 > How to move mouse cursor using C#?                                    2个
第一次在这里问一个问题,我在这里找到的解决方案似乎不是出于某种原因而起作用.我的应用程序需要在窗口变为活动状态时设置鼠标位置,我已设置功能但无法使光标属性起作用.我出于某种原因无法使用Cursor.Position或其他任何东西.我曾希望访问聊天室找到解决方案,但显然我不会说话,直到我有20个声望.

所以我在这里询问如何用类似的方式改变光标位置

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

但编译器抱怨Current,Position,Clip,Location,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);

解决方法:

您可以使用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(..)中的x和y位置SetCursorPos(..),与此类似:

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外观,您可能需要考虑边框.

标签:c,wpf,cursor-position
来源: https://codeday.me/bug/20190609/1207548.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有