如何在WPF中获取当前的鼠标屏幕坐标? [英] How do I get the current mouse screen coordinates in WPF?

查看:1116
本文介绍了如何在WPF中获取当前的鼠标屏幕坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to get current mouse coordination on the screen? I know only Mouse.GetPosition() which get mousePosition of element, but I want to get the coordination without using element.

推荐答案

跟进Rachel的回答.
您可以通过两种方式在WPF中获取鼠标屏幕坐标.

To follow up on Rachel's answer.
Here's two ways in which you can get Mouse Screen Coordinates in WPF.

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

1.Using Windows Forms. Add a reference to System.Windows.Forms

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

2.使用Win32

2.Using 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);
}

这篇关于如何在WPF中获取当前的鼠标屏幕坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆