Coordinates of location on the computer Display X = -32000, Y = -32000

I have a C # application that saves its state when it is closed, and then reads in a saved state at startup. One saved item is the location of the main form. This usually works fine - in my code there is a line like this that saves the location to a file, which is then read at startup:

streamWriter.WriteLine("location:" + this.Location.X + "," + this.Location.Y); 

Typically, the location coordinates look like this on my multi-monitor machine:

 location:-1069,283 

Sometimes I see coordinates that are saved as follows:

 location:-32000,-32000 

And then, when the user returns the application back, the form goes beyond the desktop and cannot (easily) be received by the average user.

What happens to make the coordinates read this way, and are there any suggestions to prevent this from happening?

+4
source share
2 answers

The coordinates that you see are related to the fact that the application is minimized when it is closed. Windows “hides” your form, actually moving it from its coordinates to some ridiculously large negative X, Y coordinates.

Checked by software:

Exiting the form application in Vista:

Current X coordinates: 184 Y: 184 * Default location Current X coordinates: - 32000 Y: - 32000 * Minimized location

From the code:

System.Diagnostics.Debug.WriteLine ("Current X coordinates:" + Location.X + "Y:" + Location.Y);

To solve this problem, I would simply check this value when your application closes and does not actually save it to a file.

If you do not want to interfere with the execution of mathematics with arbitrary coordinate values, you can also check WindowState. See here on MSDN

+7
source

Use the RestoreBounds property in your form.

Edit: here copy / paste part of my code (maybe you can use a little cleanup, but it gets the point). This is from my main form FormClosing event handler:

 // Save the last form state Program.AppSettings.MainWindowFormState = this.WindowState; // Check if the window is minimized or maximized if ((this.WindowState == FormWindowState.Minimized) || (this.WindowState == FormWindowState.Maximized)) { // Use the restored state values Program.AppSettings.MainWindowX = this.RestoreBounds.Left; Program.AppSettings.MainWindowY = this.RestoreBounds.Top; Program.AppSettings.MainWindowWidth = this.RestoreBounds.Width; Program.AppSettings.MainWindowHeight = this.RestoreBounds.Height; } else { // Use the normal state values Program.AppSettings.MainWindowX = this.Left; Program.AppSettings.MainWindowY = this.Top; Program.AppSettings.MainWindowWidth = this.Width; Program.AppSettings.MainWindowHeight = this.Height; } 

And here’s how to restore the state when loading (do not forget to set the form's initial position mode to “Manual” to avoid flickering):

 // Set the initial form state this.WindowState = Program.AppSettings.MainWindowFormState; this.Left = Program.AppSettings.MainWindowX; this.Top = Program.AppSettings.MainWindowY; this.Width = Program.AppSettings.MainWindowWidth; this.Height = Program.AppSettings.MainWindowHeight; 
+4
source

All Articles