Export (0) Print
Expand All

The Window Procedure

Every window must have a window procedure. The name of the window procedure is user-defined. The Generic application uses a window procedure named MainWndProc.

LRESULT CALLBACK MainWndProc( HWND, UINT, WPARAM, LPARAM );

The CALLBACK modifier is used because the window procedure must be declared with the standard call calling convention.

The window procedure receives messages from the system. These may be input messages or window-management messages. You can optionally handle a message in your window procedure or pass the message to the system 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( uMsg ) 
{
    case WM_PAINT:
    //...
    case WM_COMMAND:
    //...
    case WM_DESTROY:
    //...
    default:
    return( DefWindowProc( hWnd, uMsg, 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, TEXT("Hello, Windows!"), 15 );

         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. The system 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;

See Also

Source Code

 

 

Build date: 3/25/2010

Community Additions

Show:
© 2014 Microsoft