Determining whether a form is fully formed

I am developing an application that remembers user preferences regarding where the form was last located on the screen. In some cases, the user will have it on an additional screen, and then launches the application later without a second screen (sometimes a screen appears with the form). In other cases, the user will change his resolution, which will lead to a similar effect.

I was hoping to do this check in the Form_Shown event handler. Basically, I want to determine if the form is completely closed, so I can rearrange it.

Any tips?

+35
c # screen winforms
Jun 12 '09 at 14:45
source share
7 answers

Check this function if the form is completely on the 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; } 

Check only the upper left side if it is displayed on the 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; } 
+57
Jun 12 '09 at 14:56
source share

Combining all of the above solutions using the Intersection Using and LINQ Extensions method, let's briefly:

 public bool IsOnScreen(Form form) { // Create rectangle Rectangle formRectangle = new Rectangle(form.Left, form.Top, form.Width, form.Height); // Test return Screen.AllScreens.Any(s => s.WorkingArea.IntersectsWith(formRectangle)); } 
+15
Jul 25 '12 at 17:45
source share

Full solution here (based on all answers). I added the MinPercentOnScreen parameter, where at least this% of the pixels should be visible on all screens / displays. Therefore, if this returns false , you will need to move the default position of the window.

 // Return True if a certain percent of a rectangle is shown across the total screen area of all monitors, otherwise return False. public bool IsOnScreen(System.Drawing.Point RecLocation, System.Drawing.Size RecSize, double MinPercentOnScreen = 0.2) { double PixelsVisible = 0; System.Drawing.Rectangle Rec = new System.Drawing.Rectangle(RecLocation, RecSize); foreach (Screen Scrn in Screen.AllScreens) { System.Drawing.Rectangle r = System.Drawing.Rectangle.Intersect(Rec, Scrn.WorkingArea); // intersect rectangle with screen if (r.Width != 0 & r.Height != 0) { PixelsVisible += (r.Width * r.Height); // tally visible pixels } } return PixelsVisible >= (Rec.Width * Rec.Height) * MinPercentOnScreen; } 

Implementation:

 return IsOnScreen(this.Location, this.Size); 
+9
Apr 13 '15 at
source share

Old stream, but still useful! Cody and Andria - thanks for the code. I had to make a couple of minor adjustments: Instead of screen.WorkingArea.Intersect (formRectangle); I used formRectangle.Intersect (screen.WorkingArea); since Intersect () replaces its object with an intersection. If the form is completely disconnected from the screen, formRectangle after crossing (0,0,0,0) and Contains () returns true. Therefore, I also check if formRectangle Top, Left, Width and Height are not all 0 before returning true. Now the code returns true if any part of the form is on the screen, and false if there is no part on the screen.

+4
Dec 16 '10 at 22:08
source share

For WPF (based on Matthias Lerks answer)

Add a link to System.Windows.Forms and System.Drawing.

 //using System.Windows.Forms; public bool IsOnScreen(Window window) { var windowRect = new System.Drawing.Rectangle((int)window.Left, (int)window.Top, (int)window.Width, (int)window.Height); return Screen.AllScreens.Any(s => s.WorkingArea.IntersectsWith(windowRect)); } 
+1
Jun 04 '13 at 22:05
source share

Before installing the window, check the screen resolution. This will allow you to find out if you are going to post it outside of the permission before you do it.

0
Jun 12 '09 at 14:57
source share

None of this works if the monitor is turned off. The Screen.AllScreens function always returns the number of screens, even if one of them is turned off.

0
Apr 14 '19 at 19:03
source share



All Articles