4
votes

When aligning a WPF window of my application to the left screen border, my code returns negative values for the Left property of the window (-7, not expected). The same code returns zero on a peer's PC (as expected).

Display scaling is switched off. It is a plain 96 DPI 1920 x 1200 screen.

AFAIR it worked on my PC as expected some time ago and of course I did not change anything ;-)

So

  1. why does WPF return this value and
  2. why does it work differently on different systems and
  3. how can the 'factory settings' be retrieved?

Background: The application supports saving the window positions to a 'workbench' file and loading it again on a different system. This is difficult to achive if (0,0) is not the same on different systems. (Of course the available screens need to be considered. But this is a different stor.y)

Some more wierd details

  • System.Windows.SystemParameters.WorkArea.TopLeft is (0,0) (as expected). Other parameters of System.Windows.SystemParameters.WorkArea are also as expected (width and height).
  • Setting the position of the application window to (-7,0) moves the window to the top left corner of the screen.
  • After sizing the window to fill the screen (using the mouse) it's width is 1934 (1920 expected). Setting its width to this values resizes the window to fill the screen(width).

So there seems to be some application specific scaling and offset that is consistent in retrieving and setting window size and position but does not match System.Windows.SystemParameters.WorkArea.

1
I can only think of different OS themes (most likely different margins for window) also I found this jake.ginnivan.net/remembering-wpf-window-positionsBizhan
All machines are set to Windows 10 default settings.(Unfortunately the linked blog does not provide any information that will help me.)user2261015
I was hoping that win api might give you different valuesBizhan

1 Answers

0
votes

As a workaround the following code can be used from the post here:

 private double GetWindowLeft(Window window)
 {
      if (window.WindowState == WindowState.Maximized)
      {
          var leftField = typeof(Window).GetField("_actualLeft", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
              return (double)leftField.GetValue(window);
      }
      else
          return window.Left;
 }