1.2.14 Window Styles

Windows provides several different window styles that can be combined to form different kinds of windows. The styles are used in the CreateWindow function when the window is created.

1.2.14.1 Overlapped Windows

An overlapped window is always a top-level window. In other words, an overlapped window never has a parent window. It has a client area, a border, and a title bar. It can also have a System menu, Minimize and Maximize buttons, scroll bars, and a menu, if these items are specified when the window is created. For a window used as a main interface, the System menu and Minimize and Maximize buttons are strongly recommended.

Every overlapped window can have a corresponding icon that Windows displays when the window is minimized. A minimized window is not destroyed. It can be restored to its previous size and position. An application minimizes a window to save screen space when several windows are open at the same time.

An application creates an overlapped window by using the WS_OVERLAPPED or WS_OVERLAPPEDWINDOW style with the CreateWindow function. An overlapped window created with the WS_OVERLAPPED style always has a title bar and a border. The WS_OVERLAPPEDWINDOW style creates an overlapped window with a title bar, a thick-frame border, a System menu, and Minimize and Maximize buttons. For a complete list of window styles, see the description of the CreateWindow function in the Microsoft Windows Programmer's Reference, Volume 2.

1.2.14.2 Owned Windows

An owned window is a special type of overlapped window. Every owned window must be owned by an overlapped window. Being owned forces several constraints on a window:

An owned window is always in front of its owner when the windows are in z-order. Attempting to move the owner—that is, on an imaginary z-axis extending in front of the owned window from the screen toward the user—causes the owned window also to change position to ensure that it will always be in front of its owner.

Windows automatically destroys an owned window when it destroys the window's owner.

An owned window is hidden when its owner is minimized.

An application creates an owned window by specifying the owner's window handle as the hWndParent parameter of the CreateWindow function when creating a window that has the WS_OVERLAPPED style.

Dialog boxes are owned windows by default. The function that creates the dialog box receives the handle of the owner window as its hWndParent parameter.

1.2.14.3 Pop-up Windows

Pop-up windows are another special type of overlapped window. The main difference between a pop-up window and other overlapped windows is that an overlapped window always has a title bar, whereas the title bar is optional for a pop-up window. Like other overlapped windows, pop-up windows can be owned.

You create a pop-up window by using the WS_POPUP window style with the CreateWindow function. An application can use the ShowWindow function to open or close a pop-up window.

1.2.14.4 Child Windows

A child window is a window that is confined to the client area of a parent window. Child windows are typically used to divide the client area of a parent window into different functional areas.

You create a child window by using the WS_CHILD window style with the CreateWindow function. An application can use the ShowWindow function to show or hide a child window.

Every child window must have a parent window. The parent window can be an overlapped window, a pop-up window, or even another child window. The parent window relinquishes a portion of its client area to the child window, and the child window receives all input from this area. The window class does not have to be the same for each of the child windows of the parent window. This means an application can fill a parent window with child windows that look different and carry out different tasks.

A child window has a client area, but it does not have any other features unless these are explicitly requested. An application can request a border, title bar, Minimize and Maximize buttons, and scroll bars for a child window. In most cases, the application designs its own features for the child window.

Although it is not required, every child window should have a unique integer identifier. The identifier, given in the hmenu parameter of the CreateWindow function in place of a menu, helps identify the child window when its parent window has other child windows. The child window should use this identifier in any messages it sends to the parent window. This is the way a parent window with multiple child windows can identify which child window is sending the message. Child windows that share the same parent window are sibling windows.

Windows always positions the child window relative to the upper-left corner of the parent window's client area. The coordinates are always client coordinates. (For information about mapping, see Chapter 2, “Graphics Device Interface.”) If all or part of a child window is moved outside the visible portion of the parent window's client area, the child window is clipped; that is, the portion outside the parent window's client area is not displayed.

A child window is an independent window that receives its own input and other messages. Input intended for a child window goes directly to the child window and is not passed through the parent window. The only exception is if input to the child window has been disabled by the EnableWindow function. In this case, Windows passes any input that would have gone to the child window to the parent window instead. This gives the parent window an opportunity to examine the input and enable the child window, if necessary.

Actions that affect the parent window can also affect the child window, as follows:

Parent window Child window

Shown Shown after the parent window is shown.
Hidden Hidden before the parent window is hidden. A child window can be visible only when the parent window is visible.
Destroyed Destroyed before the parent window is destroyed.
Moved Moved with the parent window's client area. The child window is responsible for painting after the move.
Increased in size or maximized Paints any portions of the parent window that have been exposed as a result of the increased size of the client area.

Windows does not automatically clip a child window from the parent window's client area. This means the parent window draws over the child window if it carries out any drawing in the same location as the child window. Windows does clip the child window from the parent window's client area if the parent window has a WS_CLIPCHILDREN style. If the child window is clipped, the parent window cannot draw over it.

A child window can overlap other child windows in the same client area. Sibling windows can draw in each other's client area unless one child window has a WS_CLIPSIBLINGS style. If the application specifies this style for a child window, any portion of that child's sibling window that lies within this window is clipped.

If a window has either the WS_CLIPCHILDREN or WS_CLIPSIBLINGS style, a slight loss in performance occurs.

Each window takes up system resources, so an application should not use child windows indiscriminately. For optimum performance, an application that needs to logically divide its main window should do so in the window procedure of the main window rather than by using child windows.