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

  1. /*
  2.  * Function (s) demonstrated in this program: CopyRect
  3.  *
  4.  * Description:  This function copies the contents of one rectangle structure
  5.  *     into another.
  6.  *
  7.  */
  8.  
  9. #define NOMINMAX
  10. #include <windows.h>
  11. #include <stdlib.h>
  12. #include <math.h>
  13.  
  14. char    szRadius  [15];
  15. HANDLE  hInstMain;
  16. HWND    hWndMain;
  17.  
  18. long FAR PASCAL WndProc                (HWND, unsigned, WORD, LONG);
  19. void            DrawSquare             (HDC, RECT);
  20.  
  21. /****************************************************************************/
  22.  
  23. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  24. HANDLE  hInstance, hPrevInstance;
  25. LPSTR    lpszCmdLine;
  26. int    nCmdShow;
  27.   {
  28.   static char    szAppName[] = "CopyRect";
  29.   HWND    hWnd;
  30.   MSG     msg;
  31.   WNDCLASS    wndclass;
  32.  
  33.   if (!hPrevInstance)
  34.     {
  35.     wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  36.     wndclass.lpfnWndProc    = WndProc;
  37.     wndclass.cbClsExtra     = 0;
  38.     wndclass.cbWndExtra     = 0;
  39.     wndclass.hInstance      = hInstance;
  40.     wndclass.hIcon          = NULL; /*LoadIcon (NULL, IDI_ASTERISK);*/
  41.     wndclass.hCursor        = LoadCursor (NULL, IDC_CROSS);
  42.     wndclass.hbrBackground  = GetStockObject (WHITE_BRUSH);
  43.     wndclass.lpszMenuName   = NULL;
  44.     wndclass.lpszClassName  = szAppName;
  45.  
  46.     if (!RegisterClass (&wndclass))
  47.       return FALSE;
  48.     }
  49.   hWnd = CreateWindow (szAppName, "CopyRect",
  50.                       WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0,
  51.                       CW_USEDEFAULT, 0, NULL, NULL,
  52.                       hInstance, NULL);
  53.  
  54.   hInstMain = hInstance;
  55.   hWndMain  = hWnd;
  56.  
  57.   ShowWindow (hWnd, nCmdShow);
  58.   UpdateWindow (hWnd);
  59.  
  60.   while (GetMessage (&msg, NULL, 0, 0))
  61.     {
  62.     TranslateMessage (&msg);
  63.     DispatchMessage (&msg);
  64.     }
  65.   return msg.wParam;
  66.   }
  67.  
  68.  
  69. /****************************************************************************/
  70.  
  71. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  72. HWND        hWnd;
  73. unsigned    iMessage;
  74. WORD        wParam;
  75. LONG        lParam;
  76.   {
  77.   static RECT   Rect1, Rect2;
  78.   HDC            hDC;
  79.   PAINTSTRUCT   ps;
  80.   int   xOffset, yOffset;
  81.  
  82.   switch (iMessage)
  83.     {
  84.     case WM_CREATE:
  85.       SetRectEmpty (&Rect1);
  86.       SetRectEmpty (&Rect2);
  87.  
  88.       Rect1.top = 10;
  89.       Rect1.left = 15;
  90.       Rect1.right = 30;
  91.       Rect1.bottom = 20;
  92.       break;
  93.  
  94.     case WM_PAINT:
  95.       SetRectEmpty (&Rect1);
  96.       SetRectEmpty (&Rect2);
  97.       Rect1.top = 10;
  98.       Rect1.left = 15;
  99.       Rect1.right = 30;
  100.       Rect1.bottom = 20;
  101.  
  102.       hDC = BeginPaint (hWnd, &ps);
  103.       CopyRect (&Rect2, &Rect1);      /*  Copy the contents of Rect1
  104.                                        *  to Rect2  */
  105.  
  106.       for (xOffset = 0; xOffset < 15; xOffset++)
  107.         {
  108.         Rect1.top += 10;
  109.         Rect1.left += 15;
  110.         Rect1.bottom += 11;
  111.         Rect1.right += 17;
  112.         DrawSquare (hDC, Rect1);
  113.         }
  114.       DrawSquare (hDC, Rect2);
  115.       EndPaint (hWnd, &ps);
  116.       break;
  117.  
  118.     case WM_DESTROY:
  119.       PostQuitMessage (0);
  120.       break;
  121.  
  122.     default:
  123.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  124.     }
  125.   return 0L;
  126.   }
  127.  
  128.  
  129. /***************************************************************************/
  130.  
  131. void DrawSquare (hDC, rect)
  132. HDC       hDC;
  133. RECT      rect;
  134.   {
  135.   MoveTo (hDC, rect.left,  rect.top);
  136.   LineTo (hDC, rect.right, rect.top);
  137.   LineTo (hDC, rect.right, rect.bottom);
  138.   LineTo (hDC, rect.left,  rect.bottom);
  139.   LineTo (hDC, rect.left,  rect.top);
  140.   }
  141.