c#uwp指针位置 [英] c# uwp pointer position

查看:36
本文介绍了c#uwp指针位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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屋!

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