How do I get the position of a form and the screen it is displayed on, and restore that the next time my form is displayed?

巧了我就是萌 提交于 2019-12-13 01:43:44

问题


I have 2 monitors and I need to save form position and it should show on screen where it was closed.

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

The settings I save in registry.


回答1:


The simplest way is to call the GetWindowPlacement function. That returns a WINDOWPLACEMENT structure that contains information about the window's on-screen coordinates.

Using this function instead of the Form.Location property solves the problems you'll experience with multiple monitors, minimized windows, strangely positioned taskbars, etc.

The route of attack would be to call the GetWindowPlacement function when your application is shutting down, persist the location of the window to the registry (or wherever you are storing it—the registry is no longer the recommended place for saving application state), and then when your application is re-opened, calling the corresponding SetWindowPlacement function to restore the window to its previous position.

Since these are native functions exposed by the Win32 API, and you're working in C#, you'll need to call them via P/Invoke. Here are the required definitions (for organizational purposes, I recommend placing these in a static class named 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 (which you would do when your app is closing), use this code:

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

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




回答2:


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



来源:https://stackoverflow.com/questions/10511619/how-do-i-get-the-position-of-a-form-and-the-screen-it-is-displayed-on-and-resto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!