def window__activate(hwnd):
# Give the window the focus. This is the Microsoft Magic Focus Dance.
current_hwnd = windll.user32.GetForegroundWindow()
current_thread_id = windll.kernel32.GetCurrentThreadId()
thread_process_id = windll.user32.GetWindowThreadProcessId(current_hwnd, None)
if thread_process_id != current_thread_id:
res = windll.user32.AttachThreadInput(thread_process_id, current_thread_id, True)
# ERROR_INVALID_PARAMETER means that the two threads are already attached.
if res == 0 and GetLastError() != ERROR_INVALID_PARAMETER:
# TODO better logging
print("WARN: could not attach thread input to thread {0} ({1})".format(thread_process_id, GetLastError()))
return True
res = windll.user32.SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE)
if res == 0:
return False
# At this point, the window hwnd is valid, so we don't need to fail out
# if the results are non-zero. Some of these will not succeed due to
# attributes of the window, rather than the window not existing.
windll.user32.SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE)
windll.user32.AttachThreadInput(thread_process_id, current_thread_id, False)
windll.user32.SetForegroundWindow(hwnd)
windll.user32.SetFocus(hwnd)
windll.user32.SetActiveWindow(hwnd)
return True
评论列表
文章目录