How to get the position of the form and the screen on which it is displayed, and restore it the next time my form is displayed?

I have 2 monitors, and I need to save the position of the form, and it should be displayed on the screen where it was closed.

Can someone suggest how to get the screen on which it is located, and on loading the form show it on the screen where the form was closed?

Settings that I save in the registry.

+1
source share
2 answers

The easiest way is to call the GetWindowPlacement function. This returns a WINDOWPLACEMENT structure that contains information about the coordinates of the window on the screen.

Using this function instead of the Form.Location property solves the problems that you will encounter with multiple monitors, minimized windows, strange taskbars, etc.

The attack route will be to call the GetWindowPlacement function GetWindowPlacement you close your application, save the location of the window in the registry (or where you store it) - the registry is no longer the recommended place to save the state of the application), and then when your application reopens by calling the corresponding function SetWindowPlacement to restore the window to its previous position.

Since these are native functions open using the Win32 API, and you are working in C #, you need to call them through P / Invoke. Here are the required definitions (for organizational purposes, I recommend placing them in a static class called NativeMethods ):

 [StructLayout(LayoutKind.Sequential)] struct POINT { public int X; public int Y; } [StructLayout(LayoutKind.Sequential)] struct RECT { public int left; public int top; public int right; public int bottom; } [Serializable] [StructLayout(LayoutKind.Sequential)] struct WINDOWPLACEMENT { public int length; public int flags; public int showCmd; public POINT ptMinPosition; public POINT ptMaxPosition; public RECT rcNormalPosition; } [DllImport("user32", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl); [DllImport("user32", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl); 

To get the current position of your window (what will you do when your application closes), use this code:

 WINDOWPLACEMENT wp = new WINDOWPLACEMENT(); wp.length = Marshal.SizeOf(wp); GetWindowPlacement(MyForm.Handle, ref wp); 

Recall that I mentioned that the registry is no longer the recommended place to save application state. Since you work in .NET, you have much more powerful and versatile options. And since the WINDOWPLACEMENT class declared above is marked as [Serializable] , it would be very easy to serialize this information in the settings, and then reload it the next time you open it.

0
source

I have implemented similar functionality many times. All you have to do is save the properties of Form.WindowState , Form.Size and Form.Loaction when the form is closed and restore them when the form is opened.

0
source

All Articles