views:

767

answers:

3

I am developing an application that remembers the user's preferences as to where the form was last located on screen. In some instances the user will have it on a secondary screen, and then fire the app up later without the second screen (sometimes having the form appear off screen). Other times the user will change their resolution resulting in a similar effect.

I was hoping to do this checking in the Form_Shown event handler. Basically I want to determine wether the form is completly off screen so I can reposition it.

Any advice? Thanks!

A: 

Don't reposition your forms. Let the user handle that.

Randolpho
The user does reposition the form. However they have requested that the form remember its previous location. So if they have it on a secondary screen and remove the screen this error will occur.
Cody
As someone who deals with multiple monitors a lot -- and hooked to a laptop, I see this as completely the wrong approach. As many applications remember their last position, they will often come up off screen (this happens to me with Visual Studio dialogs a LOT). Then if the dialog is a modeless sub-dialog there is virtually no way to get the dialog back into the users screen.
Chris Brandsma
+11  A: 

check with this function if Form is fully on screen:

public bool IsOnScreen(Form form)
     {
      Screen[] screens = Screen.AllScreens;
      foreach (Screen screen in screens)
      {
       Rectangle formRectangle = new Rectangle(form.Left,form.Top,form.Width, form.Height);

       if (screen.WorkingArea.Contains(formRectangle))
       {
        return true;
       }
      }

      return false;
     }

checking only TopLeft point if it's on screen:

public bool IsOnScreen(Form form)
     {
      Screen[] screens = Screen.AllScreens;
      foreach (Screen screen in screens)
      {
       Point formTopLeft = new Point(form.Left, form.Top);

       if (screen.WorkingArea.Contains(formTopLeft))
       {
        return true;
       }
      }

      return false;
     }
Andrija
I forgot how to get Form rectangle other way around, but I hope this helps
Andrija
That looks really neat, and I didn't know that functionality existed. One question though: Won't that still return false if the window is only partially off screen? The question asks for completely offscreen.
Martin Harris
yes it will actually...
Andrija
I've added TopLeft point check
Andrija
Thank you Andrija. This looks like it will work with some modifications. I haven't tried it yet, but I believe I saw a screen.WorkingArea.Intersects or something similar while experimenting yesterday that will be able to tell if it is completely off screen.
Cody
I just played with it. If you add the line screen.WorkingArea.Intersect(formRectangle);right above the if statement if (screen.WorkingArea.Contains(formRectangle)) it will determine if the form is completely off screen or not.Thanks again Andrija!
Cody
A: 

Check the screens resolution before you position the window. That will allow you to figure out if you where going to place it outside the bounds of the resolution, before you actually do it.

Allen