VC中判断指定窗口是否被其他窗口遮挡

时间:2021-01-27 09:59:05

   本来是想判断当前窗口是否在最前面,无奈办法用尽就是不行,于是想换个思路:判断指定窗口是否被其他窗口遮挡。然后掘网三尺,找到了这个:

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