答案 0 :(得分:1)
汉斯一如既往地说对了。 Pinvoke是你的朋友..我找到了我的需求in the msdn forum的解决方案,这个方法使用了许多WinAPI调用来确定窗口是否重叠。我做了一些小修改,以便忽略某些句柄。只要另一个窗口稍微重叠,该方法就返回true,您可能需要进行自己的修改..
private const int GW_HWNDPREV = 3;
[DllImport("user32.dll")]
private static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
[DllImport("user32.dll")]
private static extern bool IntersectRect(out RECT lprcDst, [In] ref RECT lprcSrc1, [In] ref RECT lprcSrc2);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr hWnd);
public static bool IsWindowOverlapped(IntPtr hWnd, HashSet<IntPtr> ignoreHandles)
{
if (hWnd == IntPtr.Zero)
return false; //throw new InvalidOperationException("Window does not yet exist");
if (!IsWindowVisible(hWnd))
return false;
HashSet<IntPtr> visited = new HashSet<IntPtr> { hWnd };
//The set is used to make calling GetWindow in a loop stable by checking if we have already
//visited the window returned by GetWindow. This avoids the possibility of an infinate loop.
RECT thisRect = new RECT();
GetWindowRect(hWnd, ref thisRect);
while ((hWnd = GetWindow(hWnd, GW_HWNDPREV)) != IntPtr.Zero && !visited.Contains(hWnd))
{
visited.Add(hWnd);
if (ignoreHandles != null && ignoreHandles.Contains(hWnd))
continue;
RECT testRect, intersection;
testRect = intersection = new RECT();
if (IsWindowVisible(hWnd) && GetWindowRect(hWnd, ref testRect) && IntersectRect(out intersection, ref thisRect, ref testRect))
return true;
}
return false;
}