1

I've experienced a bug(?) in windows 10 with window positioning. I've reproduced this in WPF. Lets say I create a window like so:

var d = new Window();
d.Width = 100;
d.Height = 100;
d.Left = 1000;
d.Top = 100;
d.WindowStyle = WindowStyle.None; 
d.ResizeMode = ResizeMode.NoResize;
d.Show();

I am on resolution 1920x1080, so I'd expect the left of the window to be just right of center at 1000px (as it was in windows 8), however It's not. With testing (by both hovering the mouse, and grabbing mouse coordinates, and also by testing with PointToScreen) it shows that the X (Left) position of the window is actually at 1250 and the Y (Top) position is 125. So all of the coordinates are being adjusted by a factor of 25%. However, if you check the Window.Left property, it still says 1000 dispite the window actually being located at 1250.

I immediately went looking for display settings in windows and I found this:

enter image description here

So I figured that if this was the culprit it was just a matter of detecting this some how, except I found that even changing this to 100% instead of 125% actually makes no change and the bug is still the same.

So has anyone else ran into this bug and if it is a matter of a windows display setting, is there any way to detect it?

caesay
  • 16,932
  • 15
  • 95
  • 160
  • 1
    Did you restart or log out and back in after changing the setting? WPF does not use pixel units but rather units that are DPI dependent. – Cyral Aug 01 '15 at 23:00
  • I didn't log out or restart my computer, just the program - Although when adjusting the setting everything else on my computer seemed to update in real time to the change so I assumed it wasn't required. If a restart fixes it, that still begs the question on how you could detect this setting and adjust accordingly. – caesay Aug 01 '15 at 23:05
  • 1
    Maybe MS forgot to put a warning in the new settings app. Whenever I change DPI in control panel it prompts me to logout before the settings are applied completely. As far as for handling this, [just get the DPI](http://stackoverflow.com/questions/1918877/how-can-i-get-the-dpi-in-wpf), and get a value that you can multiply your position by. For example, `1000 * (96 / dpi)` – Cyral Aug 01 '15 at 23:08
  • 1
    @Cyral It does ask you to relogin when changing that setting, so it's just a matter of the OP not doing it – Sami Kuhmonen Aug 02 '15 at 00:00

1 Answers1

4

With help from Cyral in the comments of the question, Find the DPI as such:

var dpiX = (int)typeof(SystemParameters).GetProperty("DpiX", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
var dpiY = (int)typeof(SystemParameters).GetProperty("Dpi", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);

And then make the correction of: [Desired Pixel Coordinate] * (96 / [dpi])

This is due to the fact that WPF uses DPI and virtual pixels for layout instead of physical pixels.

caesay
  • 16,932
  • 15
  • 95
  • 160