简体   繁体   English

如何获取表单的位置和显示的屏幕,并在下次显示表单时恢复该位置?

[英]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.WindowStateForm.SizeForm.Loaction属性,并在打开表单时还原它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何判断我的表格是否显示在屏幕之外? - How can I tell if my form is displayed beyond the screen? C#Windows窗体-如何为组合框字符串分配一个双精度值,然后将其显示在文本框中? - C# Windows Form - How do I assign a double value to a combo box string to then be displayed in a text box? 我在窗体上有一个DataGridView,但在运行窗体时却没有显示。 为什么? - I have a DataGridView on my form that is not being displayed when I run my form. Why? 为什么我的表单在显示时会调整大小? - Why is my form being resized when it is displayed? 如何在屏幕上获取Windows窗体的位置? - How to get the position of a Windows Form on the screen? 什么时候显示表格? - When is a form displayed? 表单不能动态显示 - Form cannot be displayed dynamically MVC 3验证批注错误消息不会显示在字段窗体旁边 - mvc 3 validation annotation error messages are not displayed next to the fields form 在窗体显示在屏幕上N秒后,如何编程代码以执行操作? - How to program code to perform an action N seconds after the form is displayed on screen? 如何至少在显示窗体的框架之前不加载窗体的控件? - How can I make a form's controls not load until at least the frame of the form has been displayed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM