Utility to display information on all windows in z-order and their children.






4.58/5 (12 votes)
Jan 7, 2003
1 min read

78880

2915
Prints a text tree showing all windows in system, along with information about each window.
Introduction
This console program uses the GetTopWindow
and
GetNextWindow
calls recursively to iterate through
the entire tree of windows in the system. The program outputs information on these
windows in a text based tree view.
For each window, it shows the title, the registered class,
the handle of the owner process, the coordinates, and whether or not the window is visible.
Using the code
The main work of the program is done in the PrintChildrenWindows
function.
This steps through the z-order, listing all windows, and recursively steps into the
list of child windows for each window it visits. The output is a text tree that looks something
like this:
133114 "windump - Microsoft Visual C++ - [windump.cpp]"
"Afx:400000:8:10011:0:10505" 892
| (-4, -4)-(1028, 744) VISIBLE
|
+-133110 "" "Afx:400000:0:10011:10:0" 892
| (0, 723)-(1024, 740) VISIBLE
|
+-133118 "" "Afx:400000:8" 892
| | (0, 19)-(1024, 106) VISIBLE
| |
| +-67628 "" "Afx:400000:b:10011:10:0" 892
| | (367, 77)-(655, 107) VISIBLE
| |
| +-67626 "" "Afx:400000:b:10011:10:0" 892
| | (607, 47)-(733, 77) VISIBLE
| |
| +-67614 "" "Afx:400000:b:10011:10:0" 892
| | | (-1, 47)-(607, 77) VISIBLE
| | |
| | +-67616 "" "AfxWnd42" 892
| | | (193, 51)-(230, 73) VISIBLE
| | |
...
The first line displays the handle, the title, the class name, and the handle of the owner process. The second line displays the coordinates (left, top)-(right, bottom), and whether or not the window is currently visible.
// recursive algorithm // prints top level window, then tree of children windows // calling with hwnd == NULL, level == 0 starts from top of z-order void PrintChildrenWindows(HWND hwnd, int level) { hwnd = GetTopWindow(hwnd); if(!hwnd) return; while(hwnd) { char levelSpace[1024]; char nameBuffer[WINBUFF_SIZE]; char typeBuffer[WINBUFF_SIZE]; GetWindowText(hwnd, nameBuffer, WINBUFF_SIZE); RealGetWindowClassA (hwnd, typeBuffer, WINBUFF_SIZE); GenerateTreeString(levelSpace, // buffer level, // depth // are there additional siblings GetNextWindow(hwnd, GW_HWNDNEXT) ? true : false, GetTopWindow(hwnd) ? true : false, // are there children? false); // no fall through printf("%s%d \"%s\" \"%s\" %d\n",levelSpace, hwnd, nameBuffer, typeBuffer, GetWindowThreadProcessId(hwnd, NULL)); GenerateTreeString(levelSpace, // buffer level, // depth // are there more siblnigs GetNextWindow(hwnd, GW_HWNDNEXT) ? true : false, GetTopWindow(hwnd) ? true : false, // are there chidren true); // fall through RECT rect; GetWindowRect(hwnd, &rect); printf("%s (%d, %d)-(%d, %d)", levelSpace, rect.left, rect.top, rect.right, rect.bottom); if(IsWindowVisible(hwnd)) printf(" VISIBLE\n"); else printf(" HIDDEN\n"); printf("%s\n", levelSpace); PrintChildrenWindows(hwnd, level + 1); hwnd = GetNextWindow(hwnd, GW_HWNDNEXT); } }
This is the recursive function which prints the window information.
At the top level, hwnd
is NULL
.
Calling GetTopWindow(NULL)
gets the top window in the z-order.
It then loops, calling
hwnd = GetNextWindow(hwnd)
, to retrieve each successive
window in the z-order. Also, it calls
itself for each window, with hwnd
equal to the window just printed.
This causes the loop to process all child windows in the same way as
GetTopWindow(hwnd /*!= NULL*/)
gets the first child window
of hwnd
. If there are no child windows, GetTopWindow
returns NULL
, and we return, unwinding the recursion.
Points of interest
This program also contains a function called isFlagInArgs
which could easily
be extracted and used in other command line utilities to interpret command line arguments.