[英]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?
I have 2 monitors and I need to save form position and it should show on screen where it was closed. 我有2个监视器,我需要保存表单位置,它应该在关闭的屏幕上显示。
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. 我保存在注册表中的设置。
The simplest way is to call the GetWindowPlacement
function . 最简单的方法是调用
GetWindowPlacement
函数 。 That returns a WINDOWPLACEMENT
structure that contains information about the window's on-screen coordinates. 这将返回
WINDOWPLACEMENT
结构 ,该结构包含有关窗口的屏幕坐标的信息。
Using this function instead of the Form.Location
property solves the problems you'll experience with multiple monitors, minimized windows, strangely positioned taskbars, etc. 使用此函数代替
Form.Location
属性可以解决使用多个监视器,最小化窗口,任务栏位置不正确等问题。
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. 攻击途径是在应用程序关闭时调用
GetWindowPlacement
函数,将窗口的位置保留在注册表中(或存储窗口的任何位置,注册表不再是保存应用程序状态的推荐位置),并且然后在重新打开应用程序时,调用相应的SetWindowPlacement
函数以将窗口还原到其先前位置。
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. 由于这些是Win32 API公开的本机函数,并且您使用的是C#,因此需要通过P / Invoke调用它们。 Here are the required definitions (for organizational purposes, I recommend placing these in a static class named
NativeMethods
): 这是必需的定义(出于组织目的,我建议将它们放在名为
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.
由于您是在.NET中进行开发的,因此您可以使用更加强大和通用的选项。 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. 并且由于上面声明的
WINDOWPLACEMENT
类被标记为[Serializable]
,因此很容易将该信息序列化为应用程序设置 ,然后在下次打开时重新加载它。
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. 您要做的就是在关闭表单时保存
Form.WindowState
, Form.Size
和Form.Loaction
属性,并在打开表单时还原它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.