如何在 WPF 中获取当前鼠标屏幕坐标?
如何获取当前鼠标在屏幕上的坐标? 我只知道 Mouse.GetPosition()
可以获取元素的 mousePosition,但我想在不使用元素的情况下获得协调。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
或者在纯 WPF 中使用 PointToScreen。
示例辅助方法:
Or in pure WPF use PointToScreen.
Sample helper method:
跟进雷切尔的回答。
以下是在 WPF 中获取鼠标屏幕坐标的两种方法。
1.使用Windows窗体。添加对 System.Windows.Forms 的引用
2.使用 Win32
To follow up on Rachel's answer.
Here's two ways in which you can get Mouse Screen Coordinates in WPF.
1.Using Windows Forms. Add a reference to System.Windows.Forms
2.Using Win32
您想要相对于屏幕或应用程序的坐标吗?
如果它在应用程序中,只需使用:
如果没有,我相信您可以添加对 System.Windows.Forms 的引用并使用:
Do you want coordinates relative to the screen or the application?
If it's within the application just use:
If not, I believe you can add a reference to
System.Windows.Forms
and use:如果您在不同的分辨率、具有多个显示器的计算机等上尝试了很多这些答案,您可能会发现它们不能可靠地工作。这是因为您需要使用变换来获取鼠标相对于当前屏幕的位置,而不是包含所有显示器的整个查看区域。像这样的东西...(其中“this”是一个 WPF 窗口)。
If you try a lot of these answers out on different resolutions, computers with multiple monitors, etc. you may find that they don't work reliably. This is because you need to use a transform to get the mouse position relative to the current screen, not the entire viewing area which consists of all your monitors. Something like this...(where "this" is a WPF window).
这无需使用表单或导入任何 DLL 即可工作:
This works without having to use forms or import any DLLs:
您可以结合使用 TimerDispatcher(WPF 计时器模拟)和 Windows“Hooks”来从操作系统捕获光标位置。
Point 是一个轻量级的
结构
。它仅包含 X、Y 字段。该方案还解决了参数读取太频繁或太少的问题,您可以自行调整。但请记住,带有一个参数的 WPF 方法重载代表的是“刻度”而不是“毫秒”。
You may use combination of TimerDispatcher (WPF Timer analog) and Windows "Hooks" to catch cursor position from operational system.
Point is a light
struct
. It contains only X, Y fields.This solution is also resolving the problem with too often or too infrequent parameter reading so you can adjust it by yourself. But remember about WPF method overload with one arg which is representing
ticks
notmilliseconds
.如果您正在寻找 1 衬垫,这款就很适合。
+ mWindow.Left
和+ mWindow.Top
确保即使用户拖动窗口时位置也位于正确的位置。If you're looking for a 1 liner, this does well.
The
+ mWindow.Left
and+ mWindow.Top
makes sure the position is in the right place even when the user drags the window around.Mouse.GetPosition(mWindow)
为您提供相对于您选择的参数的鼠标位置。mWindow.PointToScreen()
将位置转换为相对于屏幕的点。所以
mWindow.PointToScreen(Mouse.GetPosition(mWindow))
为您提供鼠标相对于屏幕的位置,假设mWindow
是一个窗口(实际上,任何从派生的类code>System.Windows.Media.Visual
将具有此函数),如果您在 WPF 窗口类中使用此函数,则this
应该可以工作。Mouse.GetPosition(mWindow)
gives you the mouse position relative to the parameter of your choice.mWindow.PointToScreen()
convert the position to a point relative to the screen.So
mWindow.PointToScreen(Mouse.GetPosition(mWindow))
gives you the mouse position relative to the screen, assuming thatmWindow
is a window(actually, any class derived fromSystem.Windows.Media.Visual
will have this function), if you are using this inside a WPF window class,this
should work.我想使用这个代码
I wanna use this code
在纯 WPF 中获取当前窗口实例('this')的鼠标绝对坐标(在双 4K 屏幕上测试)
To get the mouse absolute coordinates (tested on dual 4K screens) in pure WPF for the current window instance ('this')