Check if the form is within all screens

I am trying to clarify if the full form is displayed on the screen. To clarify this: I don’t care if the form is partially or completely hidden by another form, I just want to know if the form is completely on the screen.

On Windows, you can move forms around so that they are half hidden. This is because you can move them past the actual borders of any monitor. (Further left, right, or bottom.) How can I check if this is easy?

I realized what I can do is to check if the form is SystemInformation.VirtualScreen . The problem here is that not all pixels of the virtual screen are actually visible. Of course, this will work if SystemInformation.MonitorCount = 1

However, I am not very happy with this.

+6
source share
2 answers

The best way I can think of is to check that all four corners of the form are on the screen. Like this:

  public bool FormOnScreen(Form frm) { if (frm.IsHandleCreated) throw new InvalidOperationException(); if (!frm.Visible || frm.WindowState == FormWindowState.Minimized) return false; return PointVisible(new Point(frm.Left, frm.Top)) && PointVisible(new Point(frm.Right, frm.Top)) && PointVisible(new Point(frm.Right, frm.Bottom)) && PointVisible(new Point(frm.Left, frm.Bottom)); } private static bool PointVisible(Point p) { var scr = Screen.FromPoint(p); return scr.Bounds.Contains(p); } 
+3
source
 Public Function IsOnScreen(ByVal form As Form) As Boolean Dim screens() As Screen = Screen.AllScreens For Each scrn As Screen In screens Dim formRectangle As Rectangle = New Rectangle(form.Left, form.Top, form.Width, form.Height) If scrn.WorkingArea.Contains(formRectangle) Then Return True End If Next Return False End Function 
+4
source

All Articles