Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

c# - Location Coordinates On Computer Showing X=-32000, Y=-32000

I have a C# application that saves its state when it is closed and then reads in the saved state when it starts up. One item that is saved is the location of the main form. Normally this works great - in my code there is a line like this that saves the location to a file that is then read back in on startup:

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

Normally, the location coordinates look like this on my machine with multiple monitors:

location:-1069,283

Occasionally I'll see coordinates that are saved like this:

location:-32000,-32000

And then when the user brings the app back up, the form is way off the desktop and can't (easily) be retrieved by an average user.

What is happening to make the coordinates be read this way and are there suggestions to prevent this situation?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The coordinates you're seeing are due to the fact that the application is minimized when it is closed. Windows "hides" your form by actually moving it from its coordinates to some ridiculously large negative X,Y coordinates.

Verified programmatically:

Output from a form app on Vista:

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

From the code:

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

To solve this I would simply check for such a value when your app is closing and not actually save it to file.

If you don't want to mess with doing math on arbitrary coordinate values you could also check the WindowState. See here on MSDN


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...