How do I get size and location of a form when then form is minimized?

  • Thread starter Thread starter John
  • Start date Start date
J

John

I want to read the size and location so I can save the window's
position so I can later restore it.

John
 
Hi John,

The problems of getting the size and location of a window
when it's minimized can be avoided by saving the data
before the window is minimized.

Try doing the following:

private void form_LocationChanged(object obj, EventArgs e)
{
if(this.WindowState == FormWindowState.Normal)
{
this.savedX = this.Location.X;
this.savedY = this.Location.Y;
}
}

private void form_LocationChanged(object obj, EventArgs e)
{
if(this.WindowState == FormWindowState.Normal)
{
this.savedHeight = this.Height;
this.savedWidth = this.Width;
}
}

Note that you will need four member variables to save the
data X, Y, Width and Height. When you load initially, do
not forget to initialize them as well. Then, before
exiting your program, save the size and location from your
member variables (ie this.savedX, this.savedY,
this.savedHeight, this.savedWidth).

Please let me know if you still have question. :)



regards,
Sean
 
Hi John,

Please ignore my first reply. There was a slight error on
that code.

The problems of getting the size and location of a window
when it's minimized can be avoided by saving the data
before the window is minimized.

Try doing the following:

private void form_LocationChanged(object obj, EventArgs e)
{
if(this.WindowState == FormWindowState.Normal)
{
this.savedX = this.Location.X;
this.savedY = this.Location.Y;
}
}

private void form_SizeChanged(object obj, EventArgs e)
{
if(this.WindowState == FormWindowState.Normal)
{
this.savedHeight = this.Height;
this.savedWidth = this.Width;
}
}

Note that you will need four member variables to save the
data X, Y, Width and Height. When you load initially, do
not forget to initialize them as well. Then, before
exiting your program, save the size and location from your
member variables (ie this.savedX, this.savedY,
this.savedHeight, this.savedWidth).

Please let me know if you still have question. :)



regards,
Sean
 
Back
Top