It's interesting to tell if a form is completely obscured, but what's the use of this trick? )

Source: Internet
Author: User

private void Form1_paint (object sender, PaintEventArgs e)
{
Text = E.cliprectangle.width.tostring ();
}

In the form's Paint event, there is a ClipRectangle property, interpreted as "get the rectangle in which you want to paint."
The function of this property is: When the form is refreshed, it is not necessary to draw any areas that are obscured to improve efficiency.
Then judging whether the form is completely obscured, it is only necessary to determine whether a valid drawing is generated when the refresh occurs.

BOOL Windowpaint = false;

private void Form1_paint (object sender, PaintEventArgs e)
{
Windowpaint = e.cliprectangle.width > 0 && e.cliprectangle.height > 0; Areas where a refresh exists
}

private void Timer1_Tick (object sender, EventArgs e)
{
Windowpaint = false;
Invalidate ();
if (windowpaint)
Text = "client area visible";
else Text = "client area is not visible";
}


Write the above code according to this idea. The results of the test are valid for the client area judgment, and the title bar judgment is invalid.
Lenovo has no parameters in Delphi OnPaint, this refresh area can be obtained through the Canvas.cliprect property.
Analyze VCL Source Code
function TCanvas.GetClipRect:TRect;
Begin
Requiredstate ([cshandlevalid]);
Getclipbox (Fhandle, Result);
End
Locate the Getclipbox function.
By experience GETWINDOWDC can get the entire form of the canvas (including customer and non-client areas);
So there is a clue, apart hands-on test it.
---Delphi----
function Windowpall (ahandle:thandle): Boolean; Whether the form is obscured
Var
Vdc:thandle;
Vrect:trect;
Begin
Result: = False;
If not iswindowvisible (ahandle) then Exit;
VDC: = GETWINDOWDC (Ahandle);
Try
Getclipbox (VDC, vrect);
Result: = (vrect.right-vrect.left <= 0) and (vrect.bottom-vrect.top <= 0);
Finally
ReleaseDC (Ahandle, VDC);
End
End {Windowpall}

Procedure Tform1.timer1timer (Sender:tobject);
Begin
Application.title: = Booltostr (Windowpall (Handle), True);
End
Achieve the desired effect. Translated into C #.

Using System.Runtime.InteropServices;

[DllImport ("User32.dll")]
public static extern bool IsWindowVisible (IntPtr hWnd);

[DllImport ("User32.dll")]
public static extern IntPtr GETWINDOWDC (IntPtr hWnd);

[DllImport ("User32.dll")]
public static extern int ReleaseDC (IntPtr hWnd, IntPtr HDC);

[DllImport ("Gdi32.dll")]
public static extern int Getclipbox (IntPtr HDC, ref Rectangle lpRect);

<summary>
Determine if the form is obscured
</summary>
<param name= "hWnd" > form handles </param>
<returns> returns whether the form is completely obscured </returns>
public bool Windowpall (INTPTR ahandle)
{
if (! IsWindowVisible (Ahandle)) return false; Form not visible
IntPtr VDC = GETWINDOWDC (Ahandle);
Try
{
Rectangle vrect = new Rectangle ();
Getclipbox (VDC, ref vrect);
Return vrect.width-vrect.left <= 0 && vrect.height-vrect.top <= 0;
Special Note: Rectangle.width corresponding API in Rect.right, Rectangle.height for Rect.bottom
}
Finally
{
ReleaseDC (Ahandle, VDC);
}
}

private void Timer1_Tick (object sender, EventArgs e)
{
Text = Windowpall (Handle). ToString ();
}


This solution does not consider the situation of irregular forms, may be related to Getcliprgn, interested friends can do their own, do not forget to share with you.

http://blog.csdn.net/zswang/article/details/2056199

It's interesting to tell if a form is completely obscured, but what's the use of this trick? )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.