Two use-cases:
Enumerate windows and then get the process handle for each window
Enumerate processes and then get the main application window handle for each process
Enumerate windows and then get the process handle for each window
You need these APIs:
win32gui.EnumWindows()
to enumerate all top-level windows (that is no child windows aka controls)win32process.GetWindowThreadProcessId()
to get process ID from window handlewin32api.OpenProcess()
to get process handle from process IDEnumerate processes and then get the main application window handle for each process
You need these APIs:
win32process.EnumProcesses()
to enumerate all processeswin32api.GetWindowLong()
with arguent GWL_STYLE
to get window styles and GWL_EXSTYLE
to get extended window styles
win32gui.GetParent()
to determine unowned windowsBy filtering the result of EnumWindows()
using GetWindowThreadProcessId()
you can get all windows that belong to a given process.
Determining the main window can be tricky as there is no single window style that would designate a window as the main window. After all, an application might have multiple main windows.
Best you could do is to use the same rules that the [taskbar uses to determine application windows][8], because that's what the user perceives as main windows:
The Shell places a button on the taskbar whenever an application creates an unowned window—that is, a window that does not have a parent and that has the appropriate extended style bits.
To ensure that the window button is placed on the taskbar, create an unowned window with the WS_EX_APPWINDOW extended style. To prevent the window button from being placed on the taskbar, create the unowned window with the WS_EX_TOOLWINDOW extended style. As an alternative, you can create a hidden window and make this hidden window the owner of your visible window.
Use GetParent()
and GetWindowLong()
to determine the unowned windows that have the right window styles according to these rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With