private
void
Form1_Paint(
object
sender,PaintEventArgse)
... {
Text=e.ClipRectangle.Width.ToString();
}
... {
Text=e.ClipRectangle.Width.ToString();
}
这个属性的作用就是:窗体在刷新的时候,为提高效率一些被遮挡的区域就不用再绘制。
那么判断窗体是否被完全遮挡,只需要判断刷新时是否产生有效绘制。
bool
windowPaint
=
false
;
private void Form1_Paint( object sender,PaintEventArgse)
... {
windowPaint=e.ClipRectangle.Width>0&&e.ClipRectangle.Height>0;//存在刷新的区域
}
private void timer1_Tick( object sender,EventArgse)
... {
windowPaint=false;
Invalidate();
if(windowPaint)
Text="客户区可见";
elseText="客户区不可见";
}
private void Form1_Paint( object sender,PaintEventArgse)
... {
windowPaint=e.ClipRectangle.Width>0&&e.ClipRectangle.Height>0;//存在刷新的区域
}
private void timer1_Tick( object sender,EventArgse)
... {
windowPaint=false;
Invalidate();
if(windowPaint)
Text="客户区可见";
elseText="客户区不可见";
}
根据这个思路写出如上代码。测试的结果是对客户区判断有效,对标题栏判断失效。
联想到Delphi中OnPaint中没有参数,这个刷新区域能通过Canvas.ClipRect属性获得。
分析VCL源代码
function TCanvas.GetClipRect: TRect;
begin
RequiredState([csHandleValid]);
GetClipBox(FHandle, Result);
end;
找到GetClipBox函数。
按经验GetWindowDC可以取得整个窗体的画布(包括客户区和非客户区);
这样就有了线索,二话不说动手测试吧。
---Delphi----
function WindowPall(AHandle: THandle): Boolean; // 窗体是否被遮住
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;
达到理想效果。翻译成C#。
using
System.Runtime.InteropServices;
[DllImport( " user32.dll " )]
public static extern bool IsWindowVisible(IntPtrhWnd);
[DllImport( " user32.dll " )]
public static extern IntPtrGetWindowDC(IntPtrhWnd);
[DllImport( " user32.dll " )]
public static extern int ReleaseDC(IntPtrhWnd,IntPtrhDC);
[DllImport( " gdi32.dll " )]
public static extern int GetClipBox(IntPtrhDC, ref RectanglelpRect);
/**/ ///<summary>
///判断窗体是否被遮挡
///</summary>
///<paramname="hWnd">窗体句柄</param>
///<returns>返回窗体是否被完全遮挡</returns>
public bool WindowPall(IntPtrAHandle)
... {
if(!IsWindowVisible(AHandle))returnfalse;//窗体不可见
IntPtrvDC=GetWindowDC(AHandle);
try
...{
RectanglevRect=newRectangle();
GetClipBox(vDC,refvRect);
returnvRect.Width-vRect.Left<=0&&vRect.Height-vRect.Top<=0;
//特别说明:Rectangle.Width对应API中RECT.Right、Rectangle.Height为RECT.Bottom
}
finally
...{
ReleaseDC(AHandle,vDC);
}
}
private void timer1_Tick( object sender,EventArgse)
... {
Text=WindowPall(Handle).ToString();
}
[DllImport( " user32.dll " )]
public static extern bool IsWindowVisible(IntPtrhWnd);
[DllImport( " user32.dll " )]
public static extern IntPtrGetWindowDC(IntPtrhWnd);
[DllImport( " user32.dll " )]
public static extern int ReleaseDC(IntPtrhWnd,IntPtrhDC);
[DllImport( " gdi32.dll " )]
public static extern int GetClipBox(IntPtrhDC, ref RectanglelpRect);
/**/ ///<summary>
///判断窗体是否被遮挡
///</summary>
///<paramname="hWnd">窗体句柄</param>
///<returns>返回窗体是否被完全遮挡</returns>
public bool WindowPall(IntPtrAHandle)
... {
if(!IsWindowVisible(AHandle))returnfalse;//窗体不可见
IntPtrvDC=GetWindowDC(AHandle);
try
...{
RectanglevRect=newRectangle();
GetClipBox(vDC,refvRect);
returnvRect.Width-vRect.Left<=0&&vRect.Height-vRect.Top<=0;
//特别说明:Rectangle.Width对应API中RECT.Right、Rectangle.Height为RECT.Bottom
}
finally
...{
ReleaseDC(AHandle,vDC);
}
}
private void timer1_Tick( object sender,EventArgse)
... {
Text=WindowPall(Handle).ToString();
}
这个解决方案没有考虑不规则窗体的情况,可能和GetClipRgn有关,有兴趣的朋友可以自己做做,做出来别忘记和大家分享一下。