1.2.21 Life Cycle of a Window

Because the purpose of any window is to let the user enter data or to let the application 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 terminated. 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 function, the application uses the CreateWindow function to create the window. This function directs Windows to prepare internal data 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. Again, the CreateWindow function sends this message to inform the window function 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 data structure, whose fields correspond to the parameters passed to CreateWindow.

Both the WM_CREATE and WM_NCCREATE messages are sent directly to the window function, bypassing the application queue. This means an application will create a window and process the WM_CREATE message before it enters the main program 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 the desired value.

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 function.

The WM_DESTROY message is usually the last message a window function processes. This occurs when the DestroyWindow function is called or when a WM_CLOSE message is processed by the DefWindowProc function. When a window function 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 terminate when the message is read from the queue.