| | | | // process the destroy message case WM_DESTROY: // Generate a quit message PostQuitMessage(0); return 0; } // Call Windows default window procedure return DefWindowProc(hwnd, iMsg, wParam, lParam); } | | | | | | If a WM_DESTROY message arrives, our window procedure sends a WM_QUIT message to terminate the thread that created the window (and thus the application if it is single-threaded). If a WM_SIZE message arrives, our window procedure grabs the width of the client area of the window (this is the portion of the window that does not include the caption or borders), which is returned in the lower 16 bits of the lParam parameter, and places it in the window's caption just for something to do. | | | | | | We should emphasize that programmers seldom call a window procedure directly. The window procedure is a callback function, that is, it is called by Windows in order to pass the message information (window handle, message ID, wParam, and lParam) to the application for processing. | | | | | | Finally, note that the final act of our window procedure is to call the default window procedure, which will supply default processing of all messages that are not processed by our window procedure (in this case, all messages except WM_SIZE and WM_DESTROY the VC++ return statement exits the window procedure). Of course, window procedures in most applications will be far more complicated than this one, possibly having code that depends upon the window that is the target of the message (as given by the hwnd parameter). In fact, for many Windows applications, the main action takes place in the window procedure! | | | | | | Once a window class is defined and registered, we can create a window based on this class. This is done using the CreateWindow function: | | HWND CreateWindow( LPCTSTR lpClassName, // pointer to registered class name LPCTSTR lpWindowName, // pointer to window name DWORD dwStyle, // window style int x, // horizontal position of window int y, // vertical position of window int nWidth, // window width int nHeight, // window height HWND hWndParent, // handle to parent to owner window HMENU hMenu, // handle to menu of child-window identifier HANDLE hInstance, // handle to application instance LPVOID lpParam // pointer to window-creation data ); |