Skip to content Skip to sidebar Skip to footer

How To Bring A Window Into Focus If It Does Not Have A Title?

We have an app that is built with openframeworks. When started, it first opens a console window that does some work (and stays open) and then starts two more child processes that e

Solution 1:

If you have unique window class name you can try GetClassName() https://msdn.microsoft.com/en-us/library/windows/desktop/ms633582(v=vs.85).aspx

Or get the window process GetWindowThreadProcessId() and check whether it's an instance of your app. See How to get the process name in C++

Solution 2:

One thought of mine was to get the parent process and then access the children from there and bring them into focus somehow, but I've not been able to figure out how."

I've been through the very similar issue, I wanted to find a child proccess with random title name, I only had the name of the executable. Here is what I've done.

import win32gui


defwindowFocusPassingPID(pid):
    defcallback(hwnd, list_to_append):
        list_to_append.append((hwnd, win32gui.GetWindowText(hwnd)))

    window_list = []
    win32gui.EnumWindows(callback, window_list)
    for i in window_list:
        print(i)  # if you want to check each itemif pid == i[0]:
            print(i)  # printing the found item (id, window_title)
            win32gui.ShowWindow(i[0], 5)
            win32gui.SetForegroundWindow(i[0])
            break# Here we will call the function, providing a valid PID
windowFocusPassingPID(INSERT_PID_HERE_IT_MUST_BE_AN_INTEGER)

So, this is a function that will call win32gui.EnumWindows, which handles all top-level windows. Those windows will be appended to the window_list, letting us know all the ID's and window names... In the loop, we will iterate over the list and match the PID you want to bring to focus... You can comment both print(i) if you want.

Here is the documentation:

http://timgolden.me.uk/pywin32-docs/win32gui__EnumWindows_meth.html

Post a Comment for "How To Bring A Window Into Focus If It Does Not Have A Title?"