def BestWindowFromPoint(point): x, y = point foundWindow = WindowFromPoint(POINT(x, y)) hWnds = GetWindowChildsList(GetAncestor(foundWindow, GA_ROOT), True) if not hWnds: return foundWindow foundWindowArea = sys.maxint rect = RECT() clientPoint = POINT() for hWnd in hWnds: GetWindowRect(hWnd, byref(rect)) if ( x >= rect.left and x <= rect.right and y >= rect.top and y <= rect.bottom ): hdc = GetDC(hWnd) clientPoint.x, clientPoint.y = x, y ScreenToClient(hWnd, byref(clientPoint)) if PtVisible(hdc, clientPoint.x, clientPoint.y): area = (rect.right - rect.left) * (rect.bottom - rect.top) if area < foundWindowArea: foundWindow = hWnd foundWindowArea = area ReleaseDC(hWnd, hdc) return foundWindow
def GetHwndChildren(hWnd, invisible): """ Return a list of all direct children of the window 'hwnd'. """ return [ childHwnd for childHwnd in GetWindowChildsList(hWnd, invisible) if GetParent(childHwnd) == hWnd ]