def BringHwndToFront(hWnd, invalidate=True): if hWnd is None: return hWnd = GetAncestor(hWnd, GA_ROOT) if not IsWindow(hWnd): return # If the window is in a minimized state, restore now if IsIconic(hWnd): ShowWindow(hWnd, SW_RESTORE) BringWindowToTop(hWnd) UpdateWindow(hWnd) # Check to see if we are the foreground thread foregroundHwnd = GetForegroundWindow() foregroundThreadID = GetWindowThreadProcessId(foregroundHwnd, None) ourThreadID = GetCurrentThreadId() # If not, attach our thread's 'input' to the foreground thread's if foregroundThreadID != ourThreadID: AttachThreadInput(foregroundThreadID, ourThreadID, True) ShowWindow(hWnd, SW_SHOWNA) BringWindowToTop(hWnd) # Force our window to redraw if invalidate: InvalidateRect(hWnd, None, True) if foregroundThreadID != ourThreadID: AttachThreadInput(foregroundThreadID, ourThreadID, False)
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