Skip to content

Instantly share code, notes, and snippets.

@dusekdan
Created March 15, 2019 16:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dusekdan/47346537bc25962b4b5a627f996a8fdf to your computer and use it in GitHub Desktop.
Save dusekdan/47346537bc25962b4b5a627f996a8fdf to your computer and use it in GitHub Desktop.
Snippet of python code to help working with Windows under win32gui.
import win32gui
import re
class WindowMgr:
"""Encapsulates some calls to the winapi for window management"""
def __init__ (self):
"""Constructor"""
self._handle = None
def __about__(self):
"""This is not my implementation. I found it somewhere on the
internet, presumably on StackOverflow.com and extended it by
the last method that returns the hwnd handle."""
pass
def find_window(self, class_name, window_name=None):
"""find a window by its class_name"""
self._handle = win32gui.FindWindow(class_name, window_name)
def _window_enum_callback(self, hwnd, wildcard):
"""Pass to win32gui.EnumWindows() to check all the opened windows"""
if re.match(wildcard, str(win32gui.GetWindowText(hwnd))) is not None:
self._handle = hwnd
def find_window_wildcard(self, wildcard):
"""find a window whose title matches the wildcard regex"""
self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)
def set_foreground(self):
"""put the window in the foreground"""
win32gui.SetForegroundWindow(self._handle)
def get_hwnd(self):
"""return hwnd for further use"""
return self._handle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment