def get_hwnd_by_title_class(self, class_text = None, title_text= None, parent_title = None,parent_class = None):
""" Returns a windows window_handler
Args:
title_text (string): the title of the window we are looking for
SPECIAL CASE: if "desktop:n" is given, a handle to the desktop number n handle is given
Returns:
int: the handler for the window if found
Raises:
win32.error: If the windowtitle is invalid
"""
if 'desktop:' in title_text.lower():
_ , num = title_text.lower().split(':',1)
num = int(num)
monitors = win32api.EnumDisplayMonitors()
tar_mon = monitors[num]
self.hwnd = tar_mon[1]
return self.hwnd
if title_text.lower() == "desktop":
self.hwnd = win32gui.GetDesktopWindow()
return self.hwnd
child_hwnd = []
def child_enumerator(hwnd,param):
child_hwnd.append(hwnd)
return True
if parent_title is not None or parent_class is not None:
logging.debug("Where supplied title/class: {0}/{1}".format(str(title_text), str(class_text)))
parent_hwnd = self.get_hwnd_by_title_class(class_text=parent_class,title_text=parent_title)
win32gui.EnumChildWindows(parent_hwnd,child_enumerator,None)
for hwnd in child_hwnd:
hwnd_title = win32gui.GetWindowText(hwnd)
hwnd_class = win32gui.GetClassName(hwnd)
if (hwnd_title == title_text and title_text is not None) or \
(hwnd_class == class_text and class_text is not None):
self.hwnd = hwnd
return hwnd
# logging.debug("Found parent with title/class {0}{1} at {2}".format(parent_title,parent_class,parent_hwnd))
# self.hwnd = win32gui.FindWindowEx(parent_hwnd,0,class_text,title_text)
else:
logging.debug("Where supplied title/class: {0}/{1}".format(str(title_text), str(class_text)))
self.hwnd = win32gui.FindWindow(class_text, title_text)
if self.hwnd == 0:
raise ValueError('Unable to find a window with that title or class')
logging.debug("Found window 0x{:2X}".format(self.hwnd))
return self.hwnd
评论列表
文章目录