Save and restore form position and size using only NormalPosition from GetWindowPlacement

182 views Asked by At

In .NET Framework 4.6 and Windows Forms, I have a borderless form, without title bar and without minimize and maximize buttons, resizable (using custom code).

I'd like to save and restore the form location and size using GetWindowPlacement and SetWindowPlacement.

I'm getting this kind of values from GetWindowPlacement:

<WindowPlacement>
  <Length>44</Length>
  <Flags>0</Flags>
  <ShowCmd>1</ShowCmd>
  <MinPosition>
    <X>-1</X>
    <Y>-1</Y>
  </MinPosition>
  <MaxPosition>
    <X>-1</X>
    <Y>-1</Y>
  </MaxPosition>
  <NormalPosition>
    <Left>949</Left>
    <Top>839</Top>
    <Right>1882</Right>
    <Bottom>1054</Bottom>
  </NormalPosition>
</WindowPlacement>

My form/window doesn't have minimize and maximize buttons, for all intended purposes it's not designed to be minimized/maximized, so it should always be restored to its normal window state.

Therefore I'm thinking only to save the NormalPosition rectangle from the WindowPlacement to application settings.

When loading the form I intent do create a WindowPlacement structure with the NormalPosition fields loaded from settings, and fill the other values with defaults (ShowCmd to 1, MinPosition to -1, -1, etc.)

What worries me is this quote from the docs:

The coordinates used in a WINDOWPLACEMENT structure should be used only by the GetWindowPlacement and SetWindowPlacement functions.

Should I be worried? Can I get some kind of weird behavior with my plan of only saving the NormalPosition?

1

There are 1 answers

0
Drake Wu On

The coordinates used in a WINDOWPLACEMENT structure should be used only by the GetWindowPlacement and SetWindowPlacement functions.

The above sentence in the document explains the reason for this problem. This is due to the different coordinate system used. If your window does not have the WS_EX_TOOLWINDOW style, rcNormalPosition contains the workspace coordinates.

Workspace coordinates differ from screen coordinates in that they take the locations and sizes of application toolbars (including the taskbar) into account. Workspace coordinate (0,0) is the upper-left corner of the workspace area, the area of the screen not being used by application toolbars.

If the taskbar is at the top of the screen, assume the height is 40, the upper left corner of the rcNormalPosition you get is (0,0), then its screen coordinates is (0,0). (The taskbar on the left is similar)

enter image description here

If this behavior affects your application, you should consider calling SetWindowPlacement with the WindowPlacement that saved when your form is loaded.