home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / graphics / valrgn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  5.8 KB  |  178 lines

  1. /*
  2.  *   ValidateRgn
  3.  *   valrgn.c
  4.  *
  5.  *   This program demonstrates the use of the ValidateRgn function. The
  6.  *   ValidateRgn function subtracts the given region from the update
  7.  *   region, which marks that region for painting. ValidateRgn is called 
  8.  *   once in this program to cancel the effects of the InvalidateRgn
  9.  *   function.  In effect, I first set a dirty bit using the InvalidateRgn
  10.  *   function.  This dirty bit tells Windows that the area containing the
  11.  *   specified region identified by the hRegion parameter needs to be
  12.  *   repainted.  I clear the dirty bit with the ValidateRgn function so
  13.  *   that the area containing the region is not repainted.  InvalidateRgn
  14.  *   is called a second time to demonstrate the effects of the first
  15.  *   ValidateRgn function.
  16.  *
  17.  */
  18.  
  19. #include "windows.h"
  20.  
  21. long FAR PASCAL ValrgnWndProc(HWND, unsigned, WORD, LONG);
  22.  
  23. /***************************************************************************/
  24.  
  25. /* Procedure called when the application is loaded for the first time */
  26. BOOL ValrgnInit( hInstance )
  27. HANDLE hInstance;
  28. {
  29.     PWNDCLASS   pValrgnClass;
  30.  
  31.     pValrgnClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  32.  
  33.     pValrgnClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  34.     pValrgnClass->hIcon          = LoadIcon( hInstance,NULL);
  35.     pValrgnClass->lpszMenuName   = (LPSTR)NULL;
  36.     pValrgnClass->lpszClassName  = (LPSTR)"ValidateRgn";
  37.     pValrgnClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  38.     pValrgnClass->hInstance      = hInstance;
  39.     pValrgnClass->style          = CS_HREDRAW | CS_VREDRAW;
  40.     pValrgnClass->lpfnWndProc    = ValrgnWndProc;
  41.  
  42.     if (!RegisterClass( (LPWNDCLASS)pValrgnClass ) )
  43.         /* Initialization failed.
  44.          * Windows will automatically deallocate all allocated memory.
  45.          */
  46.         return FALSE;
  47.  
  48.     LocalFree( (HANDLE)pValrgnClass );
  49.     return TRUE;        /* Initialization succeeded */
  50. }
  51. /***************************************************************************/
  52.  
  53. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  54. HANDLE hInstance, hPrevInstance;
  55. LPSTR lpszCmdLine;
  56. int cmdShow;
  57. {
  58.     MSG   msg;
  59.     HWND  hWnd;
  60.     HRGN  hRegion;      /* handle to elliptical region */
  61.     HDC hDC;            /* handle to display context */
  62.     HBRUSH hMyBrush;       /* handle to brush */
  63.     RECT Ellipse,       /* structure to hold ellipse coordinates */
  64.          ClearRect;     /* structure to hold client area coordinates */
  65.  
  66.     ValrgnInit( hInstance );
  67.     hWnd = CreateWindow((LPSTR)"ValidateRgn",
  68.                         (LPSTR)"ValidateRgn",
  69.             WS_OVERLAPPEDWINDOW,
  70.             CW_USEDEFAULT,
  71.             CW_USEDEFAULT,
  72.             CW_USEDEFAULT,
  73.             CW_USEDEFAULT,
  74.             (HWND)NULL,       /* no parent */
  75.                         (HMENU)NULL,       /* use class menu */
  76.                         (HANDLE)hInstance, /* handle to window instance */
  77.                         (LPSTR)NULL        /* no params to pass on */
  78.                         );
  79.  
  80.     /* Make window visible according to the way the app is activated */
  81.     ShowWindow( hWnd, cmdShow );
  82.     UpdateWindow( hWnd );
  83.  
  84. /* ----------------------------------------------------------------------- */
  85. /* Begginning of the ValidateRgn section */
  86.  
  87. /* get handle to display context */
  88.    hDC = GetDC(hWnd);
  89.  
  90. /* fill Ellipse with coordinate information for a small ellipse */
  91.    Ellipse.left = 10;
  92.    Ellipse.top  = 10;
  93.    Ellipse.right= 50;
  94.    Ellipse.bottom=50;
  95.  
  96.    GetClientRect(hWnd,(LPRECT)&ClearRect);
  97.    InvertRect(hDC,(LPRECT)&ClearRect);
  98.    hRegion = CreateEllipticRgnIndirect((LPRECT)&Ellipse);
  99.  
  100.    MessageBox(hWnd,(LPSTR)"a small ellipse, then ValidateRgn the same \
  101. small ellipse. InvalidateRgn sets a dirty bit to redraw the ellipse \
  102. region.  ValidateRgn clears the dirty bit so that a call to UpdateWindow \
  103. will not draw the small ellipse.",
  104.               (LPSTR)"I am about to InvalidateRgn...",MB_OK);
  105.  
  106.  
  107. /* invalidate the ellipse identified by hRegion (TRUE param. causes erase) */
  108.    InvalidateRgn(hWnd, (HRGN)hRegion, (BOOL)TRUE);
  109.  
  110. /* Don't draw the ellipse (ie. remove it from the update region) */
  111.    ValidateRgn(hWnd, (HRGN)hRegion);  
  112.  
  113. /* call UpdateWindow so that draw will take place immediately */
  114.    UpdateWindow(hWnd);
  115.  
  116.  
  117.    MessageBox(hWnd,(LPSTR)"Notice that the small ellipse was not drawn",
  118.               (LPSTR)"Done",MB_OK);
  119.  
  120.    MessageBox(hWnd,(LPSTR)"the small ellipse again, but this time I'm not \
  121. going to cancel the effects of InvalidateRgn with a call to ValidateRgn.  \
  122. This time my call to UpdateWindow will draw the ellipse.",
  123.               (LPSTR)"I am about to InvalidateRgn...",MB_OK);
  124.  
  125.    InvalidateRgn(hWnd, (HRGN)hRegion, (BOOL)TRUE);
  126.  
  127.    UpdateWindow(hWnd);
  128.  
  129.    ReleaseDC(hWnd, hDC);
  130.  
  131. /* End of the ValidateRgn section */
  132. /* ----------------------------------------------------------------------- */
  133.  
  134.     /* Polling messages from event queue */
  135.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  136.         TranslateMessage((LPMSG)&msg);
  137.         DispatchMessage((LPMSG)&msg);
  138.         }
  139.  
  140.     return (int)msg.wParam;
  141. }
  142. /***************************************************************************/
  143.  
  144. /* Procedures which make up the window class. */
  145. long FAR PASCAL ValrgnWndProc( hWnd, message, wParam, lParam )
  146. HWND hWnd;
  147. unsigned message;
  148. WORD wParam;
  149. LONG lParam;
  150. {
  151.     PAINTSTRUCT ps;
  152.  
  153.     switch (message)
  154.     {
  155.     case WM_SYSCOMMAND:
  156.         switch (wParam)
  157.         {
  158.         default:
  159.             return DefWindowProc( hWnd, message, wParam, lParam );
  160.         }
  161.         break;
  162.  
  163.     case WM_DESTROY:
  164.         PostQuitMessage( 0 );
  165.         break;
  166.  
  167.     case WM_PAINT:
  168.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  169.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  170.         break;
  171.  
  172.     default:
  173.         return DefWindowProc( hWnd, message, wParam, lParam );
  174.         break;
  175.     }
  176.     return(0L);
  177. }
  178.