Every window must have a window procedure. The name of the window procedure is user-defined. The GENERIC application uses the following window procedure for the main window:
LRESULT WINAPI MainWndProc( HWND, UINT, WPARAM, LPARAM );
The WINAPI modifier is used because the window procedure must be declared with the standard call calling convention.
The window procedure receives messages from Windows. These may be input messages or window-management messages from Windows. You can optionally handle a message in your window procedure or pass the message to Windows for default processing by calling the DefWindowProc function. The GENERIC application processes the WM_PAINT, WM_COMMAND, and WM_DESTROY messages, using a switch statement that is structured as follows:
switch( msg ) {
case WM_PAINT:
...
case WM_COMMAND:
...
case WM_DESTORY:
...
default:
return( DefWindowProc( hWnd, msg, wParam, lParam ));
The WM_PAINT message indicates that you should redraw what's in all or part of your application's window. Use the BeginPaint function to get a handle to a device context, then use the device context for drawing within the application's window, with functions like TextOut. Use EndPaint to release the device context. The GENERIC application displays a text string, "Hello, World!", in the window using the following code:
case WM_PAINT:
hDC = BeginPaint( hWnd, &ps );
TextOut( hDC, 10, 10, "Hello, World!", 13 );
EndPaint( hWnd, &ps );
break;
The WM_COMMAND message indicates that a user has selected a command item from a menu. The GENERIC application uses the following code to check if its About menu item has been selected:
case WM_COMMAND:
switch( wParam ) {
case IDM_ABOUT:
...
break;
}
Most window procedures process the WM_DESTROY message. Windows sends this message to the window procedure immediately after destroying the window. The message gives you the opportunity to finish processing and post a WM_QUIT message in the application queue. The GENERIC application handles the WM_DESTROY message as follows:
case WM_DESTROY:
PostQuitMessage( 0 );
break;