Skip to main content
Improved grammar and fixed some typos.
Source Link
martineau
  • 4.5k
  • 24
  • 29

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 win32guimodule 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-inctypesmodule, 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()

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" windows thatEnumWindows()enumerates over are invisible) to accomplish what you want. It makes use of the win32guimodule include in the PyWin32 extension package to gain access to the Windows API. This could also be done at a lower, more direct-level using the built-inctypesmodule 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 enhanced 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()

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 others to determine which windows might actually be visible on the desktop -- many of the "top-level" windows thatEnumWindows()enumerates over are invisible -- to accomplish what you want. It makes use of the win32guimodule included in the PyWin32 extension package to gain access to the Windows API. This can also be done at a lower, more direct, level using the built-inctypesmodule, 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 modified 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()
Added Python script showing how to use function.
Source Link
martineau
  • 4.5k
  • 24
  • 29

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" windows thatEnumWindows()enumerates over are invisible) to accomplish what you want. It makes use of the win32guimodule include in the PyWin32 extension package to gain access to the Windows API. This could also be done at a lower, more direct-level using the built-inctypesmodule 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 enhanced 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()

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 .

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" windows thatEnumWindows()enumerates over are invisible) to accomplish what you want. It makes use of the win32guimodule include in the PyWin32 extension package to gain access to the Windows API. This could also be done at a lower, more direct-level using the built-inctypesmodule 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 enhanced 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()
Post Undeleted by martineau
Post Deleted by martineau
Source Link
martineau
  • 4.5k
  • 24
  • 29

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 .