home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / RANDRECT.ZIP / RANDRECT.C < prev    next >
C/C++ Source or Header  |  1989-01-27  |  4KB  |  117 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_WINCOMMON
  8. #define INCL_WINMESSAGEMGR
  9. #define INCL_WIN
  10. #define INCL_GPI
  11. #include <os2.h>
  12. #include <stdlib.h>
  13. #define ID_TIMER 1
  14.  
  15. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  16. HAB  hab ;
  17. CHAR    szClassName[] = "RandRect";
  18.  
  19. int main (void)
  20.      {
  21.      static CHAR szClientClass[] = "RandRect" ;
  22.      HMQ         hmq ;
  23.      HWND        hwndClient, hwndFrame ;
  24.      QMSG        qmsg ;
  25.      ULONG ctldata;
  26.  
  27.  
  28.      hab = WinInitialize (NULL) ;
  29.      hmq = WinCreateMsgQueue (hab, 0) ;
  30.      WinRegisterClass( hab,
  31.                            (PCH)szClassName,
  32.                (PFNWP)ClientWndProc,
  33.                            CS_SYNCPAINT | CS_SIZEREDRAW,
  34.                0);
  35.     ctldata = FCF_STANDARD & ~FCF_MENU & ~FCF_ACCELTABLE & ~FCF_ICON;
  36.  
  37.     hwndFrame = WinCreateStdWindow( HWND_DESKTOP,
  38.                      WS_VISIBLE,
  39.                        &ctldata,
  40.                      (PCH)szClassName,
  41.                      NULL,
  42.                                          0L,
  43.                                          (HMODULE)NULL,
  44.                      0,
  45.                      (HWND FAR *)&hwndClient );
  46.  
  47.      if (hwndFrame != NULL)
  48.           {
  49.           while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  50.                WinDispatchMsg (hab, &qmsg) ;
  51.  
  52.           WinDestroyWindow (hwndFrame) ;
  53.           }
  54.      WinDestroyMsgQueue (hmq) ;
  55.      WinTerminate (hab) ;
  56.      return 0 ;
  57.      }
  58.  
  59. VOID DrawIt (HPS hps, SHORT cxClient, SHORT cyClient)
  60.      {
  61.      POINTL ptl ;
  62.  
  63.      GpiSetPattern (hps, 1L + rand() % 16) ;           // Pattern = 1 to 16
  64.      GpiSetColor (hps, (LONG) (rand() % 16)) ;         // Color = 0 to 15
  65.      GpiSetBackColor (hps, (LONG) (rand () % 16)) ;    // Background color
  66.      GpiSetBackMix (hps, BM_OVERPAINT) ;               // Background mix
  67.  
  68.      ptl.x = rand() % cxClient ;                       // First corner
  69.      ptl.y = rand() % cyClient ;
  70.      GpiMove (hps, &ptl) ;
  71.  
  72.      ptl.x = rand() % cxClient ;                       // Opposite corner
  73.      ptl.y = rand() % cyClient ;
  74.      GpiBox (hps, DRO_FILL, &ptl, 0L, 0L) ;
  75.      }
  76.  
  77. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  78.      {
  79.      static SHORT cxClient, cyClient ;
  80.      HPS          hps ;
  81.  
  82.      switch (msg)
  83.           {
  84.           case WM_CREATE:                          // Start the timer
  85.                if (!WinStartTimer (hab, hwnd, ID_TIMER, 0))
  86.                     {
  87.                     WinMessageBox (HWND_DESKTOP, hwnd,
  88.                          "Cannot run program - Too many clocks or timers",
  89.                          "RANDRECT", 0, MB_OK | MB_ICONEXCLAMATION) ;
  90.                     return 1 ;
  91.                     }
  92.                return 0 ;
  93.  
  94.           case WM_SIZE:                           // Save size of client
  95.                cxClient = SHORT1FROMMP (mp2) ;
  96.                cyClient = SHORT2FROMMP (mp2) ;
  97.                return 0 ;
  98.  
  99.           case WM_TIMER:                          // Draw a rectangle
  100.                hps = WinGetPS (hwnd) ;
  101.  
  102.                DrawIt (hps, cxClient, cyClient) ;
  103.  
  104.                WinReleasePS (hps) ;
  105.                return 0 ;
  106.  
  107.           case WM_ERASEBACKGROUND:                // Erase the background
  108.                return 1 ;
  109.  
  110.           case WM_DESTROY:                        // Stop the timer
  111.                WinStopTimer (hab, hwnd, ID_TIMER) ;
  112.                return 0 ;
  113.       default:
  114.            return( (ULONG)WinDefWindowProc(hwnd, msg, mp1, mp2));
  115.           }
  116.      }
  117.