-
-
Notifications
You must be signed in to change notification settings - Fork 608
-
On Windows, how do I get the hwnd of the window?
Beta Was this translation helpful? Give feedback.
All reactions
Answered by
RexBarker
Feb 11, 2025
This is a WindowMngr class that finds the name of your window by the title. Works if you have only one window with that name.
import re import win32gui import win32com.client class WindowMgr: """Encapsulates calls to the winapi for window management Based on: - https://stackoverflow.com/questions/2090464/python-window-activation - https://stackoverflow.com/questions/14295337/win32gui-setactivewindow-error-the-specified-procedure-could-not-be-found """ def __init__(self): self._handle = None @property def handle(self): return self._handle def _window_enum_callback(self, hwnd, wildcard): """Pass to win32gui.EnumWindows() to c...
Replies: 1 comment
-
This is a WindowMngr class that finds the name of your window by the title. Works if you have only one window with that name.
import re import win32gui import win32com.client class WindowMgr: """Encapsulates calls to the winapi for window management Based on: - https://stackoverflow.com/questions/2090464/python-window-activation - https://stackoverflow.com/questions/14295337/win32gui-setactivewindow-error-the-specified-procedure-could-not-be-found """ def __init__(self): self._handle = None @property def handle(self): return self._handle 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) return self
Use it like this:
handle = WindowMgr().find_window_wildcard("My Nice App").handle
Beta Was this translation helpful? Give feedback.
All reactions
0 replies
Answer selected by
tqphan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment