Parent-Child Relationship

Most windows have a parent window. (The exceptions are the desktop and desktop-object windows, which the system creates at system startup.) An application specifies the parent when it creates a window; then, the system uses the parent to determine where and how to draw any new windows, as well as when to destroy the windows (free all associated resources and remove the windows from the screen).

A child window is drawn relative to its parent. The coordinates given to specify the position of a window's lower-left corner are relative to the lower-left corner of its parent. For example, a main window (child of the desktop) is drawn relative to the lower-left corner of the screen (the desktop window's lower-left corner).

All main windows are siblings because they share a common parent, the desktop window. Because sibling windows can overlap, an application or a user arranges the windows, one behind another (like a stack of papers on a desk), in the desired viewing order (called z-order). Z-order uses the desktop as a reference point for a "three-dimensional" ranking of the overlapping windows: the topmost window has the highest ranking, while the window at the bottom of the stack has the lowest ranking. The parent of the sibling windows is always at the bottom of the z-order. The following figure illustrates the hierarchy of such an arrangement.

  ┌─────────┐
  │ Desktop │
  └──┬─┬─┬──┘
     │ │ │     Parent            Child
     │ │ │                  ┌─ ─ ─ ─ ─ ─ ─ ─┐
     │ │ │                       Siblings
     │ │ │   ┌────────┐     │  ┌──────────┐ │
     │ │ └──│ Main 1 ├───────│ Child 1A │
     │ │     └────────┴──┐  │  └──────────┘ │
     │ │                 │  │  ┌──────────┐
     │ │                 └────│ Child 1A │ │
     │ │                    │  └──────────┘
     │ │                    └─ ─ ─ ─ ─ ─ ─ ─┘
     │ │     ┌────────┐        ┌──────────┐
     │ └────│ Main 2 ├───────│ Child 2A │
     │       └────────┘        └──────────┘
     │       ┌────────┐
     └──────│ Main 3 │
             └────────┘

Window Hierarchy

Although PM supports z-order, it does not enforce the expected appearance unless you specify the CS_CLIPCHILDREN or CS_CLIPSIBLINGS styles. No part of a child window ever appears outside the borders of its parent. If an application creates a window that is larger than its parent, or positions a window so that some or all of it extends beyond the borders of the parent, the extended portion of the child window is not drawn.

An application can use the WS_CLIPCHILDREN or WS_CLIPSIBLINGS styles to remove from a window's clipping area (the area in which the window can paint) the area occupied by its child or sibling windows. For example, an application can use these styles to prevent a window from painting over a child or sibling window containing a complex graphic that would be time-consuming to redraw.

When a window is minimized, hidden, or destroyed, all of its children are hidden, minimized, or destroyed as well. The order of destruction is always such that every window is destroyed before its parent. The window-destruction sequence starts at the bottom of descendancy so that all related windows can be cleaned up; the last one to go is the window you asked to be destroyed. The final PM task in a window-destruction sequence is to send a WM_DESTROY message to that window, so it has one last chance to release any resources it has allocated and may still be holding.

Every window has only one parent, but can have any number of children. window in this tree is said to be a descendant of any window appearing above it in the branch, and an ancestor of any window appearing below it. There are two special cases, of course: the window immediately above is called the window's parent, and any window immediately below it is called its child. An application can change a window's parent window at any time by using the WinSetParent function. Changing the parent window also changes where and how the child window is drawn. The system displays the child within the borders of the new parent and draws the window according to the styles specified for the new parent.


[Back: Window Relationships]
[Next: Ownership]