SmallestWindowFromPoint 获得包含指定坐标的最小窗口的句柄

来源:互联网 发布:java开发相关书籍 编辑:程序博客网 时间:2024/04/30 09:44

//-----------------------------------------------
// SmallestWindowFromPoint
// Notice: from PasswordSpy by Brian Friesen
//
// Find the smallest window still containing the point
//
// WindowFromPoint returns the first window in the Z-order ->
// if the password control is sorounded by a Group Box or some other control,
// WindowFromPoint returns the handle to the sorounding control instead
// to the password control.
//
HWND SmallestWindowFromPoint( const POINT point )

 RECT rect, rcTemp;
 HWND hParent, hWnd, hTemp;

 hWnd = ::WindowFromPoint( point );
 if( hWnd != NULL )
 {
  ::GetWindowRect( hWnd, &rect );
  hParent = ::GetParent( hWnd );

  // Has window a parent?
  if( hParent != NULL )
  {
   // Search down the Z-Order
   hTemp = hWnd;
   do{
    hTemp = ::GetWindow( hTemp, GW_HWNDNEXT );

    // Search window contains the point, hase the same parent, and is visible?
    ::GetWindowRect( hTemp, &rcTemp );
    if(::PtInRect(&rcTemp, point) && ::GetParent(hTemp) == hParent && ::IsWindowVisible(hTemp))
    {
     // Is it smaller?
     if(((rcTemp.right - rcTemp.left) * (rcTemp.bottom - rcTemp.top)) < ((rect.right - rect.left) * (rect.bottom - rect.top)))
     {
      // Found new smaller window!
      hWnd = hTemp;
      ::GetWindowRect(hWnd, &rect);
     }
    }
   }while( hTemp != NULL );
  }
 }

 return hWnd;
}

原创粉丝点击