Question

I'm getting floating point values like (471, 326.723333333333) from calling MouseEventArgs.GetPosition() on my border control. While I can always round the numbers manually, I was wondering if there's some kind of setting to get whole numbers.

I thought putting SnapsToDevicePixels to True on my border control would help but it doesn't, as it's probably unrelated.

Any ideas?

Thanks!

Was it helpful?

Solution

In WPF, you'll always have positioning returned in floating point values. If you want to round it, you'll need to do this manually.

This is due to the resolution independence, and is by design.

OTHER TIPS

Since WPF is device-independent and not all devices share the same pixel sizes passing MouseEvents with fractional coordinates makes sense (also you can put controls in 3D space where the whole coordinate system translation stuff won't happen with integral pixel values).

SnapsToDevicePixels is just a property controlling how a control is displayed and that edges and straight lines preferrably lie on complete pixels than in between. It has nothing to do with how mouse events are handled (as you also have noticed).

As a general rule of thumb, everything in WPF is measured in double values, so either live with it (you checks would likely be the same anyway) or continue rounding to int if you like so :-)

Positioning from your mouse is continuous. Positioning on your monitor is discrete. The coordinate values you're getting are better than what you need and rounding will always give you the closest pixels. Basically, round it and don't worry about it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top