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 PyGetCursorPos(): """ Returns the position of the cursor, in screen co-ordinates. int x, int y = GetCursorPos() """ point = POINT() GetCursorPos(point) return point.x, point.y