本來是想判斷當前窗口是否在最前面,無奈辦法用盡就是不行,於是想換個思路:判斷指定窗口是否被其他窗口遮擋。然后掘網三尺,找到了這個:
bool CTestTray2Dlg::IsCoveredByOtherWindow(HWND hWnd)
{
RECT rcTarget;
::GetWindowRect(hWnd, &rcTarget);
bool isChild = (WS_CHILD == (::GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD));
if (::GetDesktopWindow() == hWnd)
hWnd = ::GetWindow(::GetTopWindow(hWnd), GW_HWNDLAST);
do{
HWND hCurWnd = hWnd;
while(NULL != (hWnd = ::GetNextWindow(hWnd, GW_HWNDPREV))){
if (::IsWindowVisible(hWnd)){
RECT rcWnd;
::GetWindowRect(hWnd, &rcWnd);
if(!((rcWnd.right < rcTarget.left) || (rcWnd.left > rcTarget.right) ||
(rcWnd.bottom < rcTarget.top) || (rcWnd.top > rcTarget.bottom))){
return true;
}
}
}
if(isChild){
hWnd = ::GetParent(hCurWnd);
isChild = hWnd ? (WS_CHILD == (::GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD)) : false;
}
else
break;
}while(true);
return false;
}
大意是自底向上層層遍歷窗口,檢查是否有窗口與指定窗口有重疊的地方。
試了下,還行。勉強解決了一問題,感謝網友:Pear(crabshai)
還有他的好貼:http://topic.csdn.net/u/20081007/23/4c84494d-6caa-4eb2-a2c4-2f73c67e8a63.html
http://blog.csdn.net/yuvmen/article/details/4546427
