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

  1. /*
  2. Date: 7/11/88
  3. Function(s) demonstrated in this program: GetUpdateRect
  4. Compiler version: C 5.1
  5. Description: This program paints the client area red, make the window a icon,
  6.              and then restores the window to it original size. By doing this
  7.              it creates a update region. With the update region, the function
  8.              GetUpdateRect is used to find the smallest rectangle around the 
  9.              update region. Then the program display the coordinates of the 
  10.              update rectangle.
  11. */
  12.  
  13. #include <windows.h>
  14. #include <stdio.h>
  15. #include "gupdrect.h"
  16.  
  17. static char szFuncName [] = "GetUpdateRect";
  18. static char szAppName [] = "GUPDRECT";
  19.  
  20. int PASCAL WinMain ( hInstance, hPrevInstance, lpszCmdLine, nCmdShow )
  21. HANDLE      hInstance, hPrevInstance;
  22. LPSTR       lpszCmdLine;
  23. int         nCmdShow;
  24. {
  25.      HWND        hWnd;
  26.      WNDCLASS    wndclass;
  27.      MSG         msg;
  28.      HMENU       hMenu;
  29.  
  30.      if (!hPrevInstance) {
  31.           wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  32.           wndclass.lpfnWndProc   = WndProc;
  33.           wndclass.cbClsExtra    = 0;
  34.           wndclass.cbWndExtra    = 0;
  35.           wndclass.hInstance     = hInstance;
  36.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  37.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  38.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  39.           wndclass.lpszMenuName  = "GUDR";
  40.           wndclass.lpszClassName = szFuncName;
  41.  
  42.           if (!RegisterClass (&wndclass))
  43.                return FALSE;
  44.           }
  45.  
  46.      hMenu = LoadMenu ( hInstance, "GUDR" );
  47.  
  48.  
  49.      hWnd = CreateWindow (szFuncName,           /* window class name       */
  50.                     szAppName,                  /* window caption          */
  51.                     WS_OVERLAPPEDWINDOW,        /* window style            */
  52.                     CW_USEDEFAULT,              /* initial x position      */
  53.                     0,                          /* initial y position      */
  54.                     CW_USEDEFAULT,              /* initial x size          */
  55.                     0,                          /* initial y size          */
  56.                     NULL,                       /* parent window handle    */
  57.                     hMenu,                      /* window menu handle      */
  58.                     hInstance,                  /* program instance handle */
  59.                     NULL);                      /* create parameters       */
  60.  
  61.      ShowWindow (hWnd, nCmdShow);
  62.      UpdateWindow (hWnd);
  63.  
  64.      while (GetMessage(&msg, NULL, 0, 0))
  65.      {
  66.       TranslateMessage(&msg);
  67.       DispatchMessage(&msg);
  68.      } 
  69.      return (msg.wParam);     
  70. }
  71.  
  72. long FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  73. HWND     hWnd;
  74. unsigned iMessage;
  75. WORD     wParam;
  76. LONG     lParam;
  77. {
  78.    HDC         hDC;
  79.    PAINTSTRUCT ps;
  80.    RECT        rectUpdateRect;
  81.    RECT        rectClientRect;
  82.    HBRUSH      hSolidBrush;
  83.    BOOL        bfGotUpdateRect;
  84.    int         nIndex;
  85.    char        szBuffer [100];
  86.  
  87.    switch(iMessage)
  88.    {
  89.    case WM_INITMENU:
  90.       InvalidateRect ( hWnd, (LPRECT)NULL, TRUE );
  91.       break;
  92.     case WM_COMMAND:
  93.        switch ( wParam )
  94.           {
  95.           case IDM_BOX:
  96.              GetClientRect ( hWnd, (LPRECT)&rectClientRect );
  97.      
  98.              hDC = GetDC ( hWnd );
  99.              hSolidBrush = CreateSolidBrush ( 0xFF0000 );
  100.              FillRect ( hDC, (LPRECT)&rectClientRect, hSolidBrush );
  101.              
  102.              ShowWindow ( hWnd, SW_SHOWMINIMIZED );
  103.              ShowWindow ( hWnd, SW_SHOWNORMAL );
  104.  
  105.              bfGotUpdateRect = GetUpdateRect ( hWnd, (LPRECT)&rectUpdateRect,
  106.                 FALSE );
  107.      
  108.              if ( bfGotUpdateRect == TRUE )
  109.                 {
  110.                 sprintf ( szBuffer, "%s %d%s %d%s %d%s %d%s",
  111.                    "Update rectangle .top is", rectUpdateRect.top,
  112.                    ",\nupdate rectangle .bottom is", rectUpdateRect.bottom,
  113.                    ",\nupdate rectangle .left is", rectUpdateRect.left,
  114.                    ",\nand update rectangle .right is", rectUpdateRect.right,
  115.                    "." );
  116.                 MessageBox ( hWnd, (LPSTR)szBuffer, (LPSTR)szFuncName,
  117.                    MB_OK );
  118.                 }
  119.              else
  120.                 MessageBox ( hWnd, (LPSTR)"Update rectangle is empty.",
  121.                    (LPSTR)szFuncName, MB_OK );
  122.      
  123.              ReleaseDC ( hWnd, hDC );
  124.              break;
  125.           default :
  126.              return DefWindowProc (hWnd, iMessage, wParam, lParam);
  127.              break;
  128.           }
  129.        break;
  130.      case WM_DESTROY:
  131.        PostQuitMessage(0);
  132.        break;
  133.      default:
  134.        return DefWindowProc (hWnd, iMessage, wParam, lParam);
  135.      return (0L); 
  136.    }
  137. }
  138.