Use the EnumWindows() Windows API function. It will invoke the application-defined callback function argument passed to it for all the top-level windows on the screen, passing it the handle of each one, until the callback returns FALSE
.
Here's a simple Python 2.x console program I wrote that uses that function (and a few of others to determine which windows might actually be visible on the desktop -- because many of the "top-lvel"level" windows thatEnumWindows()
enumerates over are invisible) -- to accomplish what you want. It makes use of the win32gui
module includeincluded in the PyWin32 extension package to gain access to the Windows API. This couldcan also be done at a lower, more direct-level, level using the built-inctypes
module, but PyWin32 is a more advanced and convenient way to go about it, IMO.
You could redirect the output to a text file or the program could be enhancedmodified to display the list in a window or dialog box using several different GUI modules available, including the tk/tcl interface module calledTkinter
, which comes standard with the language.
import sys
import win32gui
def callback(hwnd, strings):
if win32gui.IsWindowVisible(hwnd):
window_title = win32gui.GetWindowText(hwnd)
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
if window_title and right-left and bottom-top:
strings.append('0x{:08x}: "{}"'.format(hwnd, window_title))
return True
def main():
win_list = [] # list of strings containing win handles and window titles
win32gui.EnumWindows(callback, win_list) # populate list
for window in win_list: # print results
print window
sys.exit(0)
if __name__ == '__main__':
main()