c#uwp指针位置

编程入门 行业动态 更新时间:2024-10-28 08:16:26
本文介绍了c#uwp指针位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在 UWP 中,我试图获取指针的位置.

In UWP I am trying to get position of pointer.

我在下一个活动中成功做到了:

I have managed to do it with next Event:

    private void Grid_PointerMoved(object sender, PointerRoutedEventArgs e)
    {
        PointerPoint point = e.GetCurrentPoint(mainGrid);
        var x = point.Position.X;
        var y = point.Position.Y;
    }

通过这种方式,它会一直被触发.所以,我需要一些财产来获得那个职位.我发现了这个:

And with this way it will be fired all the time. So, I needed some property to get that position. I have found this:

var pointerPosition = Windows.UI.Core.CoreWindow.GetForCurrentThread().PointerPosition;

但它并不总是返回正确的位置.

But it doesn't always return correct position.

是否有任何其他属性可以获取当前鼠标位置?

Any other property to get current mouse location?

推荐答案

var pointerPosition = Windows.UI.Core.CoreWindow.GetForCurrentThread().PointerPosition;

pointerPosition 为您提供客户端坐标,即光标位置 X &屏幕的 Y,与您的应用窗口无关.

pointerPosition gives you the client coordinates which are the cursor position X & Y of the screen, not relative to your app Window.

所以你只需要先使用Window.Current.Bounds来找到你的应用Window的坐标,然后-

So you just need to use Window.Current.Bounds to find the coordinates of your app Window first, and then -

var x = pointerPosition.X - Window.Current.Bounds.X;
var y = pointerPosition.Y - Window.Current.Bounds.Y;

这篇关于c#uwp指针位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 09:55:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1171492.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   位置   uwp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!