home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / graphics / bgnpaint.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  4KB  |  131 lines

  1. /*
  2.  *
  3.  *   bgnpaint.c, jeffst, v1.00, 6-Dec-1987
  4.  *
  5.  *   This program demonstrates the use of the BeginPaint() function.
  6.  *   BeginPaint() prepares the given window for painting and fills the paint
  7.  *   structure with information about the painting. BeginPaint() sets the
  8.  *   clipping region to exclude any area outside the update region. This
  9.  *   is demonstrated in this program by a call to ValidateRect() immediately
  10.  *   before the BeginPaint() call. BeginPaint() is called in response to a
  11.  *   WM_PAINT message in HelloWndProc. This is standard for most window
  12.  *   procedures in Windows applications.
  13.  *
  14.  *   Microsoft Product Support Services
  15.  *   Windows Version 2.0 function demonstration application
  16.  *   Copyright (c) Microsoft 1987
  17.  *
  18.  */
  19.  
  20. #include "windows.h"
  21.  
  22. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  23.  
  24. /* Procedure called when the application is loaded for the first time */
  25. BOOL HelloInit( hInstance )
  26. HANDLE hInstance;
  27. {
  28.     PWNDCLASS   pHelloClass;
  29.  
  30.     pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  31.  
  32.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  33.     pHelloClass->hIcon        = LoadIcon( hInstance,NULL);
  34.     pHelloClass->lpszMenuName   = (LPSTR)NULL;
  35.     pHelloClass->lpszClassName    = (LPSTR)"BeginPaint";
  36.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  37.     pHelloClass->hInstance      = hInstance;
  38.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  39.     pHelloClass->lpfnWndProc    = HelloWndProc;
  40.  
  41.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  42.         /* Initialization failed.
  43.          * Windows will automatically deallocate all allocated memory.
  44.          */
  45.         return FALSE;
  46.  
  47.     LocalFree( (HANDLE)pHelloClass );
  48.     return TRUE;        /* Initialization succeeded */
  49. }
  50.  
  51.  
  52. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  53. HANDLE hInstance, hPrevInstance;
  54. LPSTR lpszCmdLine;
  55. int cmdShow;
  56. {
  57.     MSG   msg;
  58.     HWND  hWnd;
  59.     HMENU hMenu;
  60.  
  61.     HelloInit( hInstance );
  62.     hWnd = CreateWindow((LPSTR)"BeginPaint",
  63.             (LPSTR)"BeginPaint()",
  64.             WS_OVERLAPPEDWINDOW,
  65.             CW_USEDEFAULT,
  66.             CW_USEDEFAULT,
  67.             CW_USEDEFAULT,
  68.             CW_USEDEFAULT,
  69.                         (HWND)NULL,        /* no parent */
  70.                         (HMENU)NULL,       /* use class menu */
  71.                         (HANDLE)hInstance, /* handle to window instance */
  72.                         (LPSTR)NULL        /* no params to pass on */
  73.                         );
  74.  
  75.     /* Make window visible according to the way the app is activated */
  76.     ShowWindow( hWnd, cmdShow );
  77.     UpdateWindow( hWnd );
  78.  
  79.     /* Polling messages from event queue */
  80.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  81.         TranslateMessage((LPMSG)&msg);
  82.         DispatchMessage((LPMSG)&msg);
  83.         }
  84.  
  85.     return (int)msg.wParam;
  86. }
  87.  
  88. /* Procedures which make up the window class. */
  89. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  90. HWND hWnd;
  91. unsigned message;
  92. WORD wParam;
  93. LONG lParam;
  94. {
  95.     PAINTSTRUCT ps;    /* paint structure filled by BeginPaint() */
  96.     RECT rExcludeRect;    /* area to validate */
  97.     switch (message)
  98.     {
  99.     case WM_SYSCOMMAND:
  100.             return DefWindowProc( hWnd, message, wParam, lParam );
  101.         break;
  102.  
  103.     case WM_DESTROY:
  104.         PostQuitMessage( 0 );
  105.         break;
  106.  
  107.     case WM_PAINT:
  108.     rExcludeRect.top    = 0;
  109.     rExcludeRect.left   = 50;
  110.     rExcludeRect.bottom = 100;
  111.     rExcludeRect.right  = 80;
  112.    /* remove the area described by rExcludeRect from update region */
  113.     ValidateRect(hWnd,(LPRECT)&rExcludeRect);
  114.  
  115.    /****    prepare window for painting         ****/
  116.     BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  117.     TextOut(ps.hdc,
  118.         5,
  119.         5,
  120.         (LPSTR)"This line of text messed up by ValidateRect()",
  121.         45);
  122.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  123.         break;
  124.  
  125.     default:
  126.         return DefWindowProc( hWnd, message, wParam, lParam );
  127.         break;
  128.     }
  129.     return(0L);
  130. }
  131.