home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / EN0720.ZIP / RANDRECT.C < prev    next >
C/C++ Source or Header  |  1988-08-01  |  3KB  |  101 lines

  1. /*---------------------------------------------------------------
  2.    RANDRECT.C -- Random Rectangles for OS/2 Presentation Manager
  3.                  (c) 1988, Ziff Communications Co.
  4.                  PC Magazine * Charles Petzold, July 1988
  5.   ---------------------------------------------------------------*/
  6.  
  7. #define INCL_WIN
  8. #define INCL_GPI
  9. #include <os2.h>
  10. #include <stdlib.h>
  11. #define ID_TIMER 1
  12.  
  13. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  14. HAB  hab ;
  15.  
  16. int main (void)
  17.      {
  18.      static CHAR szClientClass[] = "RandRect" ;
  19.      HMQ         hmq ;
  20.      HWND        hwndClient, hwndFrame ;
  21.      QMSG        qmsg ;
  22.      ULONG       flFrameFlags = FCF_STANDARD & ~FCF_MENU ;
  23.  
  24.      hab = WinInitialize (0) ;
  25.      hmq = WinCreateMsgQueue (hab, 0) ;
  26.      WinRegisterClass (hab, szClientClass, ClientWndProc, 0L, 0) ;
  27.  
  28.      hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE,
  29.                                      &flFrameFlags, szClientClass,
  30.                                      "Random Rectangles",
  31.                                      0L, NULL, 0, &hwndClient) ;
  32.      if (hwndFrame != NULL)
  33.           {
  34.           while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  35.                WinDispatchMsg (hab, &qmsg) ;
  36.  
  37.           WinDestroyWindow (hwndFrame) ;
  38.           }
  39.      WinDestroyMsgQueue (hmq) ;
  40.      WinTerminate (hab) ;
  41.      return 0 ;
  42.      }
  43.  
  44. VOID DrawIt (HPS hps, SHORT cxClient, SHORT cyClient)
  45.      {
  46.      POINTL ptl ;
  47.  
  48.      GpiSetPattern (hps, 1L + rand() % 16) ;           // Pattern = 1 to 16
  49.      GpiSetColor (hps, (LONG) (rand() % 16)) ;         // Color = 0 to 15
  50.      GpiSetBackColor (hps, (LONG) (rand () % 16)) ;    // Background color
  51.      GpiSetBackMix (hps, BM_OVERPAINT) ;               // Background mix
  52.  
  53.      ptl.x = rand() % cxClient ;                       // First corner
  54.      ptl.y = rand() % cyClient ;
  55.      GpiMove (hps, &ptl) ;
  56.  
  57.      ptl.x = rand() % cxClient ;                       // Opposite corner
  58.      ptl.y = rand() % cyClient ;
  59.      GpiBox (hps, DRO_FILL, &ptl, 0L, 0L) ;
  60.      }
  61.  
  62. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  63.      {
  64.      static SHORT cxClient, cyClient ;
  65.      HPS          hps ;
  66.  
  67.      switch (msg)
  68.           {
  69.           case WM_CREATE:                          // Start the timer
  70.                if (!WinStartTimer (hab, hwnd, ID_TIMER, 0))
  71.                     {
  72.                     WinMessageBox (HWND_DESKTOP, hwnd,
  73.                          "Cannot run program - Too many clocks or timers",
  74.                          "RANDRECT", 0, MB_OK | MB_ICONEXCLAMATION) ;
  75.                     return 1 ;
  76.                     }
  77.                return 0 ;
  78.  
  79.           case WM_SIZE:                           // Save size of client
  80.                cxClient = SHORT1FROMMP (mp2) ;
  81.                cyClient = SHORT2FROMMP (mp2) ;
  82.                return 0 ;
  83.  
  84.           case WM_TIMER:                          // Draw a rectangle
  85.                hps = WinGetPS (hwnd) ;
  86.  
  87.                DrawIt (hps, cxClient, cyClient) ;
  88.  
  89.                WinReleasePS (hps) ;
  90.                return 0 ;
  91.  
  92.           case WM_ERASEBACKGROUND:                // Erase the background
  93.                return 1 ;
  94.  
  95.           case WM_DESTROY:                        // Stop the timer
  96.                WinStopTimer (hab, hwnd, ID_TIMER) ;
  97.                return 0 ;
  98.           }
  99.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  100.      }
  101.