Persisting form location and size for dual monitors
开发者_JS百科I have code that persists a forms size and location when it closes, and positions and sizes the form when it loads.
I want to take into account that on load it is possible that the persisted location is outside of the bounds of the screen. Easy to test in a single monitor setup but for multiple monitors, how can I test that. I don't know if the screens are set up horizontally or vertically etc. Somehow I need to determine the combined area of all screens and find out if the forms persisted location is within that area.
A good pair of functions to use for remembering and restoring window state is GetWindowPlacement
and SetWindowPlacement
. GetWindowPlacement
retrieves not only the current state and position of a normal window, but, for a maximised window, its location before it was maximised.
For your purposes, SetWindowPlacement
is useful because knows to nudge a window onto the nearest screen if it's asked to restore it off screen:
If the information specified in
WINDOWPLACEMENT
would result in a window that is completely off the screen, the system will automatically adjust the coordinates so that the window is visible, taking into account changes in screen resolution and multiple monitor configuration.
Unfortunately I don't know of a ready-made .NET equivalent of these two functions, although they're straightforward to call via P/Invoke (cf http://pinvoke.net).
Take a look at C#: How to make a form remember its Bounds and WindowState (Taking dual monitor setups into account)
精彩评论