窗体下一次启动自动设置关闭前的位置和大小。很好
很好的控制form在下一次点开后还原原来的位置和大小。老外写的很强大。

Using directives#region Using directives
using System;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Drawing;
#endregion
namespace DocToolkit

{
/**////
/// Class allows to keep last window state in Registry
/// and restore it when form is loaded.
///
/// Source: Saving and Restoring the Location, Size and
/// Windows State of a .NET Form
/// By Joel Matthias
///
/// Downloaded from http://www.codeproject.com
///
/// Using:
/// 1. Add class member to the owner form:
///
/// private PersistWindowState persistState;
///
/// 2. Create it in the form constructor:
///
/// persistState = new PersistWindowState("Software\MyCompany\MyProgram", this);
///
///
public class PersistWindowState
{
Members#region Members
private Form ownerForm; // reference to owner form
private string registryPath; // path in Registry where state information is kept
// Form state parameters:
private int normalLeft;
private int normalTop;
private int normalWidth;
private int normalHeight;
// FormWindowState is enumeration from System.Windows.Forms Namespace
// Contains 3 members: Maximized, Minimized and Normal.
private FormWindowState windowState = FormWindowState.Normal;
// if allowSaveMinimized is true, form closed in minimal state
// is loaded next time in minimal state.
private bool allowSaveMinimized = false;
#endregion

Constructor#region Constructor

/**////
/// Initialization
///
///
///
public PersistWindowState(string path, Form owner)
{
if (path == null ||
path.Length == 0)
{
registryPath = "Software\Unknown";
}
else
{
registryPath = path;
}
if (!registryPath.EndsWith("\"))
registryPath += "\";
registryPath += "MainForm";
ownerForm = owner;
// subscribe to parent form's events
ownerForm.Closing += OnClosing;
ownerForm.Resize += OnResize;
ownerForm.Move += OnMove;
ownerForm.Load += OnLoad;
// get initial width and height in case form is never resized
normalWidth = ownerForm.Width;
normalHeight = ownerForm.Height;
}
#endregion

Properties#region Properties

/**////
/// AllowSaveMinimized property (default value false)
///
public bool AllowSaveMinimized
{
get
{
return allowSaveMinimized;
}
set
{
allowSaveMinimized = value;
}
}
#endregion

Event Handlers#region Event Handlers


/**////
/// Parent form is resized.
/// Keep current size.
///
///
///
private void OnResize(object sender, System.EventArgs e)
{
// save width and height
if (ownerForm.WindowState == FormWindowState.Normal)
{
normalWidth = ownerForm.Width;
normalHeight = ownerForm.Height;
}
}

/**////
/// Parent form is moved.
/// Keep current window position.
///
///
///
private void OnMove(object sender, System.EventArgs e)
{
// save position
if (ownerForm.WindowState == FormWindowState.Normal)
{
normalLeft = ownerForm.Left;
normalTop = ownerForm.Top;
}
// save state
windowState = ownerForm.WindowState;
}

/**////
/// Parent form is closing.
/// Keep last state in Registry.
///
///
///
private void OnClosing(object sender, System.ComponentModel.CancelEventArgs e)
{
// save position, size and state
RegistryKey key = Registry.CurrentUser.CreateSubKey(registryPath);
key.SetValue("Left", normalLeft);
key.SetValue("Top", normalTop);
key.SetValue("Width", normalWidth);
key.SetValue("Height", normalHeight);
// check if we are allowed to save the state as minimized (not normally)
if (!allowSaveMinimized)
{
if (windowState == FormWindowState.Minimized)
windowState = FormWindowState.Normal;
}
key.SetValue("WindowState", (int)windowState);
}

/**////
/// Parent form is loaded.
/// Read last state from Registry and set it to form.
///
///
///
private void OnLoad(object sender, System.EventArgs e)
{
// attempt to read state from registry
RegistryKey key = Registry.CurrentUser.OpenSubKey(registryPath);
if (key != null)
{
int left = (int)key.GetValue("Left", ownerForm.Left);
int top = (int)key.GetValue("Top", ownerForm.Top);
int width = (int)key.GetValue("Width", ownerForm.Width);
int height = (int)key.GetValue("Height", ownerForm.Height);
FormWindowState windowState = (FormWindowState)key.GetValue("WindowState", (int)ownerForm.WindowState);
ownerForm.Location = new Point(left, top);
ownerForm.Size = new Size(width, height);
ownerForm.WindowState = windowState;
}
}
#endregion
}
}

/**//*下面是如何使用的方法, 使用时把namespace空间给他改了。DocToolkit。或者使用时引用using DocToolkit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowState
{
public partial class Form1 : Form
{
private PersistWindowState persistState;
const string registryPath = "Software\cxf\WindowsState";
public Form1()
{
InitializeComponent();
persistState = new PersistWindowState(registryPath, this);
}
}
}*/

浙公网安备 33010602011771号