IterateChildWindows illustrates how to traverse the window tree with GetWindow. Table 6-3 on the facing page describes some of the other window
relationships.
Except for “owner,” all these relationships should be obvious. The owner of a window is the window that is notified when something happens to the first window. You can get a better feel for this by browsing the outline in WinWatch’s Window Hierarchy box. Most windows in the hierarchy don’t have an owner, but a dialog box is owned by the window that launched it. The main Visual Basic window containing the menus and the toolbar is the owner of all the other windows—toolbox, project, properties, and code windows.
If you look at the internal data stored for a window (see “Inside CWindow” page 301), you’ll see that only three relatives are stored for each window: the parent, the next sibling, and the first child. (The owner isn’t a relative, strictly speaking.) You won’t see a first, last, or previous sibling, even though Windows provides the GW_HWNDFIRST, GW_HWNDLAST, and GW_HWNDPREV constants. Each of these relationships can be calculated from the given data. For example, the first sibling is actually the first child of the parent. The last sibling is the first sibling’s next sibling’s next sibling until the sibling with no next.
Operation | Description |
GetWindow with GW_HWNDFIRST | Get the first sibling |
GetWindow with GW_HWNDLAST | Get the last sibling |
GetWindow with GW_HWNDNEXT | Get the next sibling |
GetWindow with GW_HWNDPREV | Get the previous sibling |
GetWindow with GW_CHILD | Get the first child |
GetWindow with GW_OWNER | Get the owner |
GetNextWindow | Get the next or previous sibling |
GetTopWindow | Get the top-level window of a specified window |
GetDesktopWindow | Get the desktop (the top of the window tree) |
GetParent | Get the parent |
SetParent | Change the parent of a window |
IsChild | Indicate whether two windows are parent and child |
Table 6-3. Window relationships.