.netc++-cliwindow-position

How can I programmatically determine the size of my desktop?


I'm trying to save / restore the window size, state and position of a System::Windows::Forms::Form and want to make sure that if the window was previously on another monitor and the app is started up when only a snogle monitor is available, that it puts itself somewhere on-screen.

So how can I determine the current desktop size and whether my application is out of bounds?


Solution

  • Actually, the best solution would be to use the native methods GetWindowPlacement and SetWindowPlacement in user32.dll. These automatically handle all the edge cases.

    A sample WPF implementation using them can be found at http://jake.ginnivan.net/2010/04/remembering-wpf-windows-position-2/ From that you could do the same for WinForms.