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

  1. /*
  2.  *   InvalidateRect
  3.  *
  4.  *   This program demonstrates the use of the InvalidateRect function. The
  5.  *   InvalidateRect function adds the given rectangle to the update region,
  6.  *   which marks that rectangle for painting. InvalidateRect is called
  7.  *   twice in this program: once with a specified rectangle identified by
  8.  *   the lpRect parameter, and once with NULL sent as the parameter
  9.  *   representing the rectangle to invalidate, which causes the whole
  10.  *   client area to be invalidated. Both times the "bErase" paramter is
  11.  *   TRUE which causes the given rectangles to be erased. Also, both times
  12.  *   InvalidateRect is called, UpdateWindow is called immediately after to
  13.  *   ensure that the rectangle is immediately redrawn.
  14.  */
  15.  
  16. #include "windows.h"
  17.  
  18. long    FAR PASCAL HelloWndProc (HWND, unsigned, WORD, LONG);
  19.  
  20. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  21. HANDLE hInstance, hPrevInstance;
  22. LPSTR lpszCmdLine;
  23. int    cmdShow;
  24.   {
  25.   MSG   msg;
  26.   HWND  hWnd;
  27.   HMENU hMenu;
  28.  
  29.   DWORD rectcolor = RGB (0x22, 0x22, 0x22); /* color for the brush   */
  30.   HDC hDC;         /* handle to display context            */
  31.   HBRUSH hMyBrush;     /* handle to brush                */
  32.   RECT lpRect;       /* structure to hold rectangle coordinates */
  33.  
  34.   if (!hPrevInstance)
  35.     {
  36.     WNDCLASS   HelloClass;
  37.  
  38.     HelloClass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  39.     HelloClass.hIcon          = LoadIcon (hInstance, NULL);
  40.     HelloClass.lpszMenuName   = (LPSTR)NULL;
  41.     HelloClass.lpszClassName  = (LPSTR)"Sample Application";
  42.     HelloClass.hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH);
  43.     HelloClass.hInstance      = hInstance;
  44.     HelloClass.style          = CS_HREDRAW | CS_VREDRAW;
  45.     HelloClass.lpfnWndProc    = HelloWndProc;
  46.  
  47.     if (!RegisterClass ( (LPWNDCLASS)&HelloClass))
  48.       return FALSE;
  49.     }
  50.  
  51.   hWnd = CreateWindow ( (LPSTR)"Sample Application", (LPSTR)"Sample Application",
  52.       WS_OVERLAPPEDWINDOW,
  53.       CW_USEDEFAULT, 0,
  54.       CW_USEDEFAULT, 0,
  55.       NULL, NULL, hInstance, NULL);
  56.  
  57.   ShowWindow (hWnd, cmdShow);
  58.   UpdateWindow (hWnd);
  59.  
  60. /* create brush to fill rectangle with */
  61.   hMyBrush = CreateSolidBrush (rectcolor);
  62.  
  63. /* get handle to display context */
  64.   hDC = GetDC (hWnd);
  65.  
  66. /* select brush into display context */
  67.   SelectObject (hDC, hMyBrush);
  68.  
  69. /* draw a rectangle so we can see InvalidateRect's effects */
  70.   Rectangle (hDC, 5, 5, 650, 250);
  71.  
  72. /* fill lpRect with coordinate information */
  73.   lpRect.left = 10;
  74.   lpRect.top  = 10;
  75.   lpRect.right = 50;
  76.   lpRect.bottom = 50;
  77.  
  78. /* notify user call to InvalidateRect about to occur */
  79.   MessageBox (GetFocus (), (LPSTR)"a small rectangle",
  80.       (LPSTR)"I am about to InvalidateRect...", MB_OK);
  81.  
  82. /* invalidate the rect.identified by lpRect (TRUE param. causes erase) */
  83.   InvalidateRect (hWnd, (LPRECT) & lpRect, (BOOL)TRUE);
  84.  
  85. /* call UpdateWindow so that draw will take place immediately */
  86.   UpdateWindow (hWnd);
  87.  
  88. /* notify user call to InvalidateRect about to occur */
  89.   MessageBox (GetFocus (), (LPSTR)"the whole client area",
  90.       (LPSTR)"I am about to InvalidateRect...", MB_OK);
  91.  
  92. /* invalidate the client area (TRUE param. causes erase) */
  93.   InvalidateRect (hWnd, (LPRECT)NULL, (BOOL)TRUE);
  94.  
  95. /* call UpdateWindow so that draw will take place immediately */
  96.   UpdateWindow (hWnd);
  97.  
  98.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  99.     {
  100.     TranslateMessage ( (LPMSG) & msg);
  101.     DispatchMessage ( (LPMSG) & msg);
  102.     }
  103.   return (int)msg.wParam;
  104.   }
  105.  
  106. /* Procedures which make up the window class. */
  107. long    FAR PASCAL HelloWndProc (hWnd, message, wParam, lParam)
  108. HWND     hWnd;
  109. unsigned message;
  110. WORD     wParam;
  111. LONG     lParam;
  112.   {
  113.   switch (message)
  114.     {
  115.     case WM_DESTROY:
  116.       PostQuitMessage (0);
  117.       break;
  118.  
  119.     default:
  120.       return DefWindowProc (hWnd, message, wParam, lParam);
  121.       break;
  122.     }
  123.   return (0L);
  124.   }
  125.