When a .NET forms-based application is launched, the default behavior is to display it in a cascading fashion from the upper left corner of the screen down towards the middle.

This is useful if you want to open multiple copies of your application, but less so if your users like to have windows arranged in a particular way.

So we can easily add code to the Load and FormClosing events to store and reload the window position from the user settings.

private void Form1_Load(object sender, EventArgs e)
{
// Restore the window state and size from the settings file
if (((int)FormWindowState.Normal) ==
Properties.Settings.Default.frmMainWindowState)
{
WindowState = FormWindowState.Normal;

if ((Properties.Settings.Default.frmMainWidth > 0) &&
(Properties.Settings.Default.frmMainHeight > 0))
{
Left = Properties.Settings.Default.frmMainLeft;
Width = Properties.Settings.Default.frmMainWidth;
Top = Properties.Settings.Default.frmMainTop;
Height = Properties.Settings.Default.frmMainHeight;
}
WindowState = FormWindowState.Normal;
}
else if (((int)FormWindowState.Maximized) ==
Properties.Settings.Default.frmMainWindowState)
{
WindowState = FormWindowState.Maximized;
}
}

 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.frmMainLeft = Left;
Properties.Settings.Default.frmMainWidth = Width;
Properties.Settings.Default.frmMainTop = Top;
Properties.Settings.Default.frmMainHeight = Height;
Properties.Settings.Default.Save();
}

This code worked great during the 1990s.  But what if your user is using their laptop at their desk with it hooked up to a second monitor, then they leave and your window is “stuck” on a monitor that no longer exists?  Before moving our window to outer darkness, we can use an API function EnumDisplayMonitors to make sure that this window position at least overlaps a monitor that currently exists.

The attached class (EnsureWindowVisible.cs) uses this function in EnsureRectVisible to first check to see if the given window position overlaps at least one monitor and, if it does not, to grab the location of the first monitor and move the window onto it.

We can modify our Load function to call EnsureRectVisible to validate that the window is going to be visible before displaying it:

private void Form1_Load(object sender, EventArgs e)
{
// Restore the window state and size from the settings file
if (((int)FormWindowState.Normal) ==
Properties.Settings.Default.frmMainWindowState)
{
WindowState = FormWindowState.Normal;

if ((Properties.Settings.Default.frmMainWidth > 0) &&
(Properties.Settings.Default.frmMainHeight > 0))
{
int intLeft = Properties.Settings.Default.frmMainLeft;
int intWidth = Properties.Settings.Default.frmMainWidth;
int intTop = Properties.Settings.Default.frmMainTop;
int intHeight = Properties.Settings.Default.frmMainHeight;

EnsureWindowVisible.EnsureRectVisible(ref intLeft, ref intWidth,
ref intTop, ref intHeight);

Left = intLeft;
Width = intWidth;
Top = intTop;
Height = intHeight;
}
WindowState = FormWindowState.Normal;
}
else if (((int)FormWindowState.Maximized) ==
Properties.Settings.Default.frmMainWindowState)
{
WindowState = FormWindowState.Maximized;
}
}

For this and other cool tech tips, contact our eBusiness Solutions team. We love talking cool, new, fun technologies and methods.

image credit: Steve W

Enjoy Your 30-day Free Trial!
inbound marketing software free trial Learn how Inbound Marketing Software can help your business grow! 

Click here to get started with your free trial.

Comments are closed.