Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# uwp pointer position

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?

like image 696
user3239349 Avatar asked Jul 04 '17 11:07

user3239349


1 Answers

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

pointerPosition gives you the client coordinates which are the cursor position X & Y of the screen, not relative to your app 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;
like image 63
Justin XL Avatar answered Oct 08 '22 16:10

Justin XL