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

时间:2010-11-19 15:57:42

标签: wpf mouse-coordinates

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

9 个答案:

答案 0 :(得分:64)

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

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

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

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

答案 1 :(得分:63)

或者在纯WPF中使用PointToScreen

示例助手方法:

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

答案 2 :(得分:26)

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

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

Mouse.GetPosition(Application.Current.MainWindow);

如果没有,我相信您可以添加对System.Windows.Forms的引用并使用:

System.Windows.Forms.Control.MousePosition;

答案 3 :(得分:18)

如果您在不同的分辨率,具有多个显示器的计算机等上尝试了很多这些答案,您可能会发现它们无法可靠地工作。这是因为您需要使用变换来获取相对于当前屏幕的鼠标位置,而不是包含所有监视器的整个查看区域。像这样......(“这个”是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);
}

答案 4 :(得分:4)

无需使用表单或导入任何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);
    }

答案 5 :(得分:1)

您可以使用TimerDispatcher(WPF计时器模拟)和Windows&#34; Hooks&#34;的组合。从操作系统中捕捉光标位置。

SpringApplication

点是轻型 [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetCursorPos(out POINT pPoint); 。它只包含X,Y字段。

struct

此解决方案也可以解决问题,过于频繁或过于频繁的参数读取,因此您可以自行调整。但请记住WPF方法重载,其中一个arg表示 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; } } 而不是ticks

milliseconds

答案 6 :(得分:1)

如果您要寻找1班轮,这很好。

new Point(Mouse.GetPosition(mWindow).X + mWindow.Left, Mouse.GetPosition(mWindow).Y + mWindow.Top)

答案 7 :(得分:0)

我将其添加为用于控件

   private void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
 var mousepos = new List<double> { e.GetPosition(ControlName).X, e.GetPosition(ControlName).Y };

}

答案 8 :(得分:0)

Mouse.GetPosition(mWindow)为您提供鼠标相对于您选择的参数的位置。 mWindow.PointToScreen()将位置转换为相对于屏幕的点。

因此,mWindow.PointToScreen(Mouse.GetPosition(mWindow))为您提供了相对于屏幕的鼠标位置,假设mWindow是一个窗口(实际上,从System.Windows.Media.Visual派生的任何类都将具有此功能),如果您在WPF窗口类中使用它,this应该可以工作。

相关问题