问题描述
我有2台显示器,我需要保存位置并应当在屏幕上显示它被关闭。
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?
我保存的注册表设置。
推荐答案
最简单的方法是调用的。返回一个 WINDOWPLACEMENT
结构包含有关窗口的屏幕坐标信息。
The simplest way is to call the GetWindowPlacement
function. That returns a WINDOWPLACEMENT
structure that contains information about the window's on-screen coordinates.
使用此功能,而不是 Form.Location
物业解决问题,你会用多台显示器,最小化的窗口,奇怪的是位于任务栏等等。
Using this function instead of the Form.Location
property solves the problems you'll experience with multiple monitors, minimized windows, strangely positioned taskbars, etc.
攻击的路线将是打电话的时候,您的应用程序关闭 GetWindowPlacement
功能,坚持了窗口的位置到注册表(或无论你在哪里存放-the注册表不再保存应用程序状态推荐的地方),然后当你的应用程序被重新打开,调用相应的窗口恢复到previous位置。
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.
由于这些是由Win32 API暴露原生功能,以及你的工作在C#中,你需要通过P / Invoke来调用它们。这里有必要的定义(用于组织的目的,我建议在名为静态类将这些 NativeMethods
):
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);
要得到你的窗口的当前位置(你会在你的程序是,收盘价做),使用code:
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);
这是我提到的注册表召回不再是坚持应用程序状态的推荐的地方。既然你正在开发的.NET,你有更多的强大和灵活的选项。而且,由于 WINDOWPLACEMENT
上述声明的类被标记 [Serializable接口]
,这将是很容易的信息序列化到你应用程序设置,然后重新装入,下一次你打开。
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.
这篇关于我如何获得一个表格,并显示在屏幕上的位置,并恢复显示下一次我的形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!