Jeff Stong
Jeff Stong

Reputation: 1556

How to convert WPF MouseDevice.GetPosition(null) to screen coordinates?

How can you convert the System.Windows.Point that is returned by MouseDevice.GetPosition(null), which is relative to a WPF window into screen coordinates.

There's this hack (How to get control location in screen coordinate system) but it's almost over two years old and will only work under certain constraints. As that article describes this is not trivial to get right so I'd like to rely on a WPF method if possible.

I'm using .NET Framework 4.0 so I am not worried about backward compatibility with earlier .NET framework versions.

Upvotes: 1

Views: 4035

Answers (1)

Daniel Earwicker
Daniel Earwicker

Reputation: 116724

You need this:

http://msdn.microsoft.com/en-us/library/system.windows.media.visual.pointtoscreen.aspx

If you call it on a control and pass it (0, 0), it gives you the top-left corner of the control in screen coordinates.

Or you can pass it (ActualWidth, ActualHeight) and you'll get the bottom-right corner.

Upvotes: 4

Related Questions