def get_hwnds(pid):
"""return a list of window handlers based on it process id"""
def callback(hwnd, hwnds):
if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
_, found_pid = win32process.GetWindowThreadProcessId(hwnd)
if found_pid == pid:
hwnds.append(hwnd)
return True
hwnds = []
win32gui.EnumWindows(callback, hwnds)
return hwnds
python类IsWindowVisible()的实例源码
def __init__(self, window_name=None, exe_file=None, exclude_border=True):
hwnd = 0
# first check window_name
if window_name is not None:
hwnd = win32gui.FindWindow(None, window_name)
if hwnd == 0:
def callback(h, extra):
if window_name in win32gui.GetWindowText(h):
extra.append(h)
return True
extra = []
win32gui.EnumWindows(callback, extra)
if extra: hwnd = extra[0]
if hwnd == 0:
raise WindowsAppNotFoundError("Windows Application <%s> not found!" % window_name)
# check exe_file by checking all processes current running.
elif exe_file is not None:
pid = find_process_id(exe_file)
if pid is not None:
def callback(h, extra):
if win32gui.IsWindowVisible(h) and win32gui.IsWindowEnabled(h):
_, p = win32process.GetWindowThreadProcessId(h)
if p == pid:
extra.append(h)
return True
return True
extra = []
win32gui.EnumWindows(callback, extra)
#TODO: get main window from all windows.
if extra: hwnd = extra[0]
if hwnd == 0:
raise WindowsAppNotFoundError("Windows Application <%s> is not running!" % exe_file)
# if window_name & exe_file both are None, use the screen.
if hwnd == 0:
hwnd = win32gui.GetDesktopWindow()
self.hwnd = hwnd
self.exclude_border = exclude_border
def handle_window(hwnd,extra):#TB_handle??
if win32gui.IsWindowVisible(hwnd):
if extra in win32gui.GetWindowText(hwnd):
global handle
handle= hwnd
def __init__(self, window_name=None, exe_file=None, exclude_border=True):
hwnd = 0
# first check window_name
if window_name is not None:
hwnd = win32gui.FindWindow(None, window_name)
if hwnd == 0:
def callback(h, extra):
if window_name in win32gui.GetWindowText(h):
extra.append(h)
return True
extra = []
win32gui.EnumWindows(callback, extra)
if extra: hwnd = extra[0]
if hwnd == 0:
raise WindowsAppNotFoundError("Windows Application <%s> not found!" % window_name)
# check exe_file by checking all processes current running.
elif exe_file is not None:
pid = find_process_id(exe_file)
if pid is not None:
def callback(h, extra):
if win32gui.IsWindowVisible(h) and win32gui.IsWindowEnabled(h):
_, p = win32process.GetWindowThreadProcessId(h)
if p == pid:
extra.append(h)
return True
return True
extra = []
win32gui.EnumWindows(callback, extra)
#TODO: get main window from all windows.
if extra: hwnd = extra[0]
if hwnd == 0:
raise WindowsAppNotFoundError("Windows Application <%s> is not running!" % exe_file)
# if window_name & exe_file both are None, use the screen.
if hwnd == 0:
hwnd = win32gui.GetDesktopWindow()
self.hwnd = hwnd
self.exclude_border = exclude_border
def FindMainWindow():
win_caption = 'autoxd_frame'
hwnd = 0
for i in range(10):
if hwnd == 0:
hwnd = win32gui.FindWindow(None, win_caption)
if win32gui.IsWindowVisible(hwnd) == True:
break
hwnd = win32gui.FindWindowEx(0, hwnd , None, win_caption)
if win32gui.IsWindowVisible(hwnd) == True:
break
return hwnd
def enumHandler(hwnd, lParam):
if win32gui.IsWindowVisible(hwnd):
if 'WinGuake - Guake For Windows' in win32gui.GetWindowText(hwnd):
m_width = win32api.GetSystemMetrics(0)
m_length = win32api.GetSystemMetrics(1)
w_width = int(m_width)
w_length = int(m_length/2)
win32gui.MoveWindow(hwnd, 0, 0, w_width, w_length, True)
def enumHandler(hwnd, lParam):
if win32gui.IsWindowVisible(hwnd):
if 'WinGuake' in win32gui.GetWindowText(hwnd):
m_width = win32api.GetSystemMetrics(0)
m_length = win32api.GetSystemMetrics(1)
w_width = int(m_width)
w_length = int(m_length/2)
win32gui.MoveWindow(hwnd, 0, 0, w_width, w_length, True)
def enumHandler(hwnd, lParam):
if win32gui.IsWindowVisible(hwnd):
if 'WinGuake - Guake For Windows' in win32gui.GetWindowText(hwnd):
m_width = GetSystemMetrics(0)
m_length = GetSystemMetrics(1)
w_width = int(m_width)
w_length = int(m_length/2)
win32gui.MoveWindow(hwnd, 0, 0, w_width, w_length, True)