1.2.21 Life Cycle of a Window

Because the purpose of any window is to make it possible for the user to specify data or for the application to display information, a window starts its life cycle when the application has a need for input or output. A window continues its life cycle until there is no longer a need for it or the application is closed. Some windows, such as the window used for the application's main user interface, last the life of the application. Other windows, such as a window used as a dialog box, may last only a few seconds.

The first step in a window's life cycle is creation. Given a registered window class with a corresponding window procedure, the application uses the CreateWindow function to create the window. This function directs Windows to prepare internal structures for the window and to return a unique integer value, called a window handle, that the application can use to identify the window in subsequent function calls.

The first message most windows process is WM_CREATE, the window-creation message. The CreateWindow function sends this message to inform the window procedure that it can now perform any initialization, such as allocating memory and preparing data files. The wParam parameter is not used, but the lParam parameter contains a long pointer to a CREATESTRUCT structure, whose members correspond to the parameters passed to CreateWindow.

The WM_CREATE message is sent directly to the window procedure, bypassing the application's message queue. This means an application creates a window and processes the WM_CREATE message before it enters the main message loop.

After a window has been created, it must be opened (displayed) before it can be used. An application can open the window in one of two ways: It can specify the WS_VISIBLE window style in the CreateWindow function to open the window immediately after creation, or it can wait until later and call the ShowWindow function to open the window. When creating a main window, an application should not specify WS_VISIBLE, but should call ShowWindow from the WinMain function with the nCmdShow parameter set to specify the window state.

When the window is no longer needed or the application is terminated, the window must be destroyed. This is done by using the DestroyWindow function. DestroyWindow removes the window from the system display and invalidates the window handle. It also sends WM_DESTROY and WM_NCDESTROY messages to the window procedure. The DestroyWindow function also destroys all of the window's child and owned windows.

The window procedure also receives a WM_DESTROY message when the WM_CLOSE message is processed by the DefWindowProc function. When a window procedure receives a WM_DESTROY message, it should free any allocated memory and close any open data files.

The window used as the application's main user interface should always be the last window destroyed and should always cause the application to terminate. When this window receives a WM_DESTROY message, it should call the PostQuitMessage function. This function copies a WM_QUIT message to the application's message queue as a signal for the application to close when the message is read from the queue.