111,113
社区成员




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>
/// 判断窗体是否被遮挡
/// </summary>
/// <param name="hWnd">窗体句柄</param>
/// <returns>返回窗体是否被完全遮挡</returns>
public bool WindowPall(IntPtr AHandle)
{
if (!IsWindowVisible(AHandle)) return false; // 窗体不可见
IntPtr vDC = GetWindowDC(AHandle);
try
{
Rectangle vRect = new Rectangle();
GetClipBox(vDC, ref vRect);
return vRect.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, EventArgs e)
{
Text = WindowPall(Handle).ToString();
}
bool windowPaint = false;
private void Form1_Paint(object sender, PaintEventArgs e)
{
windowPaint = e.ClipRectangle.Width > 0 && e.ClipRectangle.Height > 0; // 存在刷新的区域
}
private void timer1_Tick(object sender, EventArgs e)
{
windowPaint = false;
Invalidate();
if (windowPaint)
Text = "客户区可见";
else Text = "客户区不可见";
}
For Each mnu As MenuItem In mnuWindows.MenuItems
If mnu.Checked Then
MessageBox.Show(mnu.Text & "窗口是激活状态!")
Else
MessageBox.Show(mnu.Text & "窗口是非激活状态!")
End If
Next