You can learn a lot about Windows by iterating through each window in the system and writing down its attributes. The window hierarchy is usually described as a tree, although it’s diagrammed as something more like a root system. I’m told that programmers in many countries draw their trees growing upward, as the name implies. Figure 6-3 shows a tree upside down, the American way. At the top is the desktop window. Below are the top-level windows, the main windows of running programs. Below each top-level window is its own set of child windows—dialog boxes, MDI windows, buttons, and so on.
Figure 6-3. A window tree.
The approved way to traverse the window tree is to use EnumChildWindows to traverse each window and its children, starting at an arbitrary window. If you want to traverse all windows in the system, you start at the desktop window. You couldn’t use EnumChildWindows in previous versions of Visual Basic, but the AddressOf operator makes it easy in version 5. “The Ultimate Hack: Procedure Pointers” in Chapter 2 tells how to do this. The problem with EnumChildWindows is that it provides no easy way to map where you are in the window hierarchy. So just to prove we can do it (and to get a little more flexibility), let’s iterate windows the hard way.