home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap12 / randrect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  3.0 KB  |  103 lines

  1. /*------------------------------------------
  2.    RANDRECT.C -- Displays Random Rectangles
  3.                  (c) Charles Petzold, 1990
  4.   ------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdlib.h>
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  10. void DrawRectangle (HWND) ;
  11.  
  12. short cxClient, cyClient ;
  13.  
  14. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  15.                     LPSTR lpszCmdLine, int nCmdShow)
  16.      {
  17.      static char szAppName[] = "RandRect" ;
  18.      HWND        hwnd ;
  19.      MSG         msg ;
  20.      WNDCLASS    wndclass ;
  21.  
  22.      if (!hPrevInstance) 
  23.           {
  24.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  25.           wndclass.lpfnWndProc   = WndProc ;
  26.           wndclass.cbClsExtra    = 0 ;
  27.           wndclass.cbWndExtra    = 0 ;
  28.           wndclass.hInstance     = hInstance ;
  29.           wndclass.hIcon         = NULL ;
  30.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  31.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  32.           wndclass.lpszMenuName  = NULL ;
  33.           wndclass.lpszClassName = szAppName ;
  34.  
  35.           !RegisterClass (&wndclass) ;
  36.           }
  37.  
  38.      hwnd = CreateWindow (szAppName, "Random Rectangles",
  39.                           WS_OVERLAPPEDWINDOW,
  40.                           CW_USEDEFAULT, CW_USEDEFAULT,
  41.                           CW_USEDEFAULT, CW_USEDEFAULT,
  42.                           NULL, NULL, hInstance, NULL) ;
  43.  
  44.      ShowWindow (hwnd, nCmdShow) ;
  45.      UpdateWindow (hwnd) ;
  46.  
  47.      while (TRUE)
  48.           {
  49.           if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  50.                {
  51.                if (msg.message == WM_QUIT)
  52.                     break ;
  53.  
  54.                TranslateMessage (&msg) ;
  55.                DispatchMessage (&msg) ;
  56.                }
  57.           else
  58.                DrawRectangle (hwnd) ;
  59.           }
  60.      return msg.wParam ;
  61.      }
  62.  
  63. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  64.      {
  65.      switch (message)
  66.           {
  67.           case WM_SIZE:
  68.                cxClient = LOWORD (lParam) ;
  69.                cyClient = HIWORD (lParam) ;
  70.                return 0 ;
  71.  
  72.           case WM_DESTROY:
  73.                PostQuitMessage (0) ;
  74.                return 0 ;
  75.           }
  76.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  77.      }
  78.  
  79. void DrawRectangle (HWND hwnd)
  80.      {
  81.      HBRUSH hBrush ;
  82.      HDC    hdc ;
  83.      short  xLeft, xRight, yTop, yBottom, nRed, nGreen, nBlue ;
  84.  
  85.      xLeft   = rand () % cxClient ;
  86.      xRight  = rand () % cxClient ;
  87.      yTop    = rand () % cyClient ;
  88.      yBottom = rand () % cyClient ;
  89.      nRed    = rand () & 255 ;
  90.      nGreen  = rand () & 255 ;
  91.      nBlue   = rand () & 255 ;
  92.  
  93.      hdc = GetDC (hwnd) ;
  94.      hBrush = CreateSolidBrush (RGB (nRed, nGreen, nBlue)) ;
  95.      SelectObject (hdc, hBrush) ;
  96.  
  97.      Rectangle (hdc, min (xLeft, xRight), min (yTop, yBottom),
  98.                      max (xLeft, xRight), max (yTop, yBottom)) ;
  99.  
  100.      ReleaseDC (hwnd, hdc) ;
  101.      DeleteObject (hBrush) ;
  102.      }     
  103.