如何在WPF中获取当前鼠标屏幕坐标?

Pri*_*ief 68 wpf mouse-coordinates

如何在屏幕上获得当前的鼠标协调?我只知道Mouse.GetPosition()哪个获取了元素的mousePosition,但我想在不使用元素的情况下获得协调.

eri*_*ikH 72

或者在纯WPF中使用PointToScreen.

样本助手方法:

// Gets the absolute mouse position, relative to screen
Point GetMousePos(){
    return _window.PointToScreen(Mouse.GetPosition(_window))
}
Run Code Online (Sandbox Code Playgroud)

  • Mouse.GetPosition返回一个Point,PointToScreen将该点转换为屏幕坐标. (15认同)
  • 这个.干净整洁. (2认同)

Fre*_*lad 71

跟进雷切尔的回答.
这里有两种方法可以在WPF中获得鼠标屏幕坐标.

1.使用Windows窗体.添加对System.Windows.Forms的引用

public static Point GetMousePositionWindowsForms()
{
    System.Drawing.Point point = Control.MousePosition;
    return new Point(point.X, point.Y);
}
Run Code Online (Sandbox Code Playgroud)

2.使用Win32

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);

[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
{
    public Int32 X;
    public Int32 Y;
};
public static Point GetMousePosition()
{
    Win32Point w32Mouse = new Win32Point();
    GetCursorPos(ref w32Mouse);
    return new Point(w32Mouse.X, w32Mouse.Y);
}
Run Code Online (Sandbox Code Playgroud)

  • 正确无误,但如果您使用原始X和Y坐标并使用它们来设置窗口的位置,则DPI设置不是100%(96dpi)时,它将无法正常工作。问题的最后答案是正确的! (2认同)

Rac*_*hel 29

您想要相对于屏幕或应用程序的坐标吗?

如果它在应用程序中,只需使用:

Mouse.GetPosition(Application.Current.MainWindow);
Run Code Online (Sandbox Code Playgroud)

如果没有,我相信你可以添加一个引用System.Windows.Forms并使用:

System.Windows.Forms.Control.MousePosition;
Run Code Online (Sandbox Code Playgroud)

  • 它必须是 System.Windows.Forms.Control.MousePosition (2认同)

Ale*_*dru 19

如果您在不同的分辨率,具有多个显示器的计算机等上尝试了很多这些答案,您可能会发现它们无法可靠地工作.这是因为您需要使用变换来获取相对于当前屏幕的鼠标位置,而不是包含所有监视器的整个查看区域.这样的东西......(其中"this"是WPF窗口).

var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var mouse = transform.Transform(GetMousePosition());

public System.Windows.Point GetMousePosition()
{
    System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
    return new System.Windows.Point(point.X, point.Y);
}
Run Code Online (Sandbox Code Playgroud)


小智 5

无需使用表单或导入任何DLL即可运行:

   using System.Windows;
   using System.Windows.Input;

    /// <summary>
    /// Gets the current mouse position on screen
    /// </summary>
    private Point GetMousePosition()
    {
        // Position of the mouse relative to the window
        var position = Mouse.GetPosition(Window);

        // Add the window position
        return new Point(position.X + Window.Left, position.Y + Window.Top);
    }
Run Code Online (Sandbox Code Playgroud)


Max*_*der 5

您可以结合使用 TimerDispatcher(WPF 计时器模拟)和 Windows“Hooks”来从操作系统捕获光标位置。

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetCursorPos(out POINT pPoint);
Run Code Online (Sandbox Code Playgroud)

点是光struct。它仅包含 X、Y 字段。

    public MainWindow()
    {
        InitializeComponent();

        DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
        dt.Tick += new EventHandler(timer_tick);
        dt.Interval = new TimeSpan(0,0,0,0, 50);
        dt.Start();
    }

    private void timer_tick(object sender, EventArgs e)
    {
        POINT pnt;
        GetCursorPos(out pnt);
        current_x_box.Text = (pnt.X).ToString();
        current_y_box.Text = (pnt.Y).ToString();
    }

    public struct POINT
    {
        public int X;
        public int Y;

        public POINT(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
    }
Run Code Online (Sandbox Code Playgroud)

该方案还解决了参数读取过于频繁或过于频繁的问题,您可以自行调整。但请记住,带有一个表示ticksnot 的参数的 WPF 方法重载milliseconds

TimeSpan(50); //ticks
Run Code Online (Sandbox Code Playgroud)