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
?
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.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)If this behavior affects your application, you should consider calling
SetWindowPlacement
with theWindowPlacement
that saved when your form is loaded.