Creating, Enumerating, and Sizing Child Windows

You can divide a window's client area into different functional areas by using child windows. Creating a child window is like creating a main window ¾ you use the CreateWindowEx function. To create a window of an application-defined window class, you must register the window class and provide a window procedure before creating the child window. You must give the child window the WS_CHILD style and specify a parent window for the child window when you create it.

The following example divides the client area of an application's main window into three functional areas by creating three child windows of equal size. Each child window is the same height as the main window's client area, but each is one-third its width. The main window creates the child windows in response to the WM_CREATE message, which the main window receives during its own window-creation process. Because each child window has the WS_BORDER style, each has a thin line border. Also, because the WS_VISIBLE style is not specified, each child window is initially hidden. Notice also that each child window is assigned a child-window identifier.

The main window sizes and positions the child windows in response to the WM_SIZE message, which the main window receives when its size changes. In response to WM_SIZE, the main window retrieves the dimensions of its client area by using the GetWindowRect function and then passes the dimensions to the EnumChildWindows function. EnumChildWindows passes the handle of each child window, in turn, to the application-defined EnumChildProc callback function. This function sizes and positions each child window by calling the MoveWindow function; the size and position are based on the dimensions of the main window's client area and the identifier of the child window. Afterward, EnumChildProc calls the ShowWindow function to make the window visible.

#define ID_FIRSTCHILD 100

#define ID_SECONDCHILD 101

#define ID_THIRDCHILD 102

LONG APIENTRY MainWndProc(hwnd, uMsg, wParam, lParam)

HWND hwnd;

UINT uMsg;

UINT wParam;

LONG lParam;

{

RECT rcClient;

int i;

switch(uMsg) {

case WM_CREATE: // creating main window

// Create three invisible child windows.

for (i = 0; i < 3; i++)

CreateWindowEx(

0,

"ChildWClass",

(LPCTSTR) NULL,

WS_CHILD | WS_BORDER,

0,0,0,0,

hwnd,

(HMENU) (int) (ID_FIRSTCHILD + i),

hinst,

NULL);

return 0;

case WM_SIZE: // main window changed size

// Get the dimensions of the main window's client

// area, and enumerate the child windows. Pass the

// dimensions to the child windows during enumeration.

GetClientRect(hwnd, &rcClient);

EnumChildWindows(hwnd, EnumChildProc,

(LPARAM) &rcClient);

return 0;

.

. // Process other messages.

.

}

return DefWindowProc(hwnd, uMsg, wParam, lParam);

}

BOOL CALLBACK EnumChildProc(hwndChild, lParam)

HWND hwndChild;

LPARAM lParam;

{

LPRECT rcParent;

int i, idChild;

// Retrieve the child-window identifier. Use it to set the

// position of the child window.

idChild = GetWindowLong(hwndChild, GWL_ID);

if (idChild == ID_FIRSTCHILD)

i = 0;

else if (idChild == ID_SECONDCHILD)

i = 1;

else

i = 2;

// Size and position the child window.

rcParent = (LPRECT) lParam;

MoveWindow(hwndChild,

(rcParent->right / 3) * i,

0,

rcParent->right / 3,

rcParent->bottom,

TRUE);

// Make sure the child window is visible.

ShowWindow(hwndChild, SW_SHOW);

return TRUE;

}