/// <summary>
/// Restores the last saved position/state of given form from the registry.
/// </summary>
/// <param name="pfrm">Form instance</param>
public static void RestoreWindowPos(Form pfrm, int left = 0, int top = 0, int width = 0, int height = 0, FormWindowState windowState = FormWindowState.Normal)
{
RegistryKey key = RegistryUtil.GetUserKey(pfrm);
try {
pfrm.Location = new Point((int)key.GetValue("WindowPos_WindowX", left), (int)key.GetValue("WindowPos_WindowY", top));
if (pfrm.FormBorderStyle == FormBorderStyle.Sizable || pfrm.FormBorderStyle == FormBorderStyle.SizableToolWindow)
{
pfrm.Size = new Size((int)key.GetValue("WindowPos_WindowWidth", width), (int)key.GetValue("WindowPos_WindowHeight", height));
}
if (pfrm.MaximizeBox)
{
pfrm.WindowState = (FormWindowState)key.GetValue("WindowPos_WindowState", windowState);
}
}
catch (Exception) {
if (left > 0 && top > 0)
{
pfrm.Location = new Point(left, top);
}
if (pfrm.FormBorderStyle == FormBorderStyle.Sizable || pfrm.FormBorderStyle == FormBorderStyle.SizableToolWindow)
{
if (width > 0 && height > 0)
{
pfrm.Size = new Size(width, height);
}
}
if (pfrm.MaximizeBox)
{
pfrm.WindowState = windowState;
}
}
finally {
key.Close();
}
if (pfrm.FormBorderStyle == FormBorderStyle.Sizable || pfrm.FormBorderStyle == FormBorderStyle.SizableToolWindow)
{
if (pfrm.Width > SystemInformation.VirtualScreen.Width)
{
pfrm.Width = SystemInformation.VirtualScreen.Width - 20;
}
if (pfrm.Height > SystemInformation.VirtualScreen.Height)
{
pfrm.Height = SystemInformation.VirtualScreen.Height - 20;
}
}
if (pfrm.Left + pfrm.Width > SystemInformation.VirtualScreen.Width)
{
pfrm.Left = SystemInformation.VirtualScreen.Width - pfrm.Width - 10;
}
if (pfrm.Top + pfrm.Height > SystemInformation.VirtualScreen.Height)
{
pfrm.Top = SystemInformation.VirtualScreen.Height - pfrm.Height - 10;
}
}