mrob95 / pyvda

Python module for manipulating Windows 10/11 windows and virtual desktops.
MIT License
90 stars 13 forks source link

Support getting HWND from window title #3

Closed CompuGenius-Programs closed 2 years ago

CompuGenius-Programs commented 2 years ago

I think it would be helpful to add a way of getting a window's HWND from it's title, so that it can be used to automate moving windows in more scenarios.

I currently implemented this as follows:

def move_window_by_name(hwnd, l_param):
    if win32gui.IsWindowVisible(hwnd):
        if 'Window Title' in win32gui.GetWindowText(hwnd):
            AppView(hwnd).move(VirtualDesktop(2))

win32gui.EnumWindows(move_window_by_name, None)

I'm sure there's a way to add this into the main module, I just didn't look into it. Or, at least maybe put this code in the README in case anyone else wants it.

mrob95 commented 2 years ago

I'm reluctant to start adding functions like this because it's usually fairly simple to filter windows using normal win32 calls, and every user is going to want to filter a different way (exact title match, containing match, regex match, etc..)

I'd be happy to merge a PR that adds more examples to the readme though. Something like this?

def move_window_by_name(title: str, target_desktop: int):
    """
        Move any window whose title equals 'title' to virtual desktop number 'target_desktop'
    """
    ...