home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP04 / SCRAMBLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  2.2 KB  |  67 lines

  1. /*------------------------------------------------
  2.    SCRAMBLE.C -- Scramble (and Unscramble) Screen
  3.                  (c) Charles Petzold, 1996
  4.   ------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdlib.h>
  8.  
  9. #define NUM 200
  10.  
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15.      {
  16.      static int iKeep [NUM][4] ;
  17.      HDC        hdc, hdcMem ;
  18.      int        cx, cy ;
  19.      HBITMAP    hBitmap ;
  20.      int        i, j, x1, y1, x2, y2 ;
  21.  
  22.      if (LockWindowUpdate (GetDesktopWindow ()))
  23.           {
  24.           hdc     = CreateDC ("DISPLAY", NULL, NULL, NULL) ;
  25.           hdcMem  = CreateCompatibleDC (hdc) ;
  26.           cx  = GetSystemMetrics (SM_CXSCREEN) / 10 ;
  27.           cy  = GetSystemMetrics (SM_CYSCREEN) / 10 ;
  28.           hBitmap = CreateCompatibleBitmap (hdc, cx, cy) ;
  29.  
  30.           SelectObject (hdcMem, hBitmap) ;
  31.  
  32.           srand ((int) GetCurrentTime ()) ;
  33.  
  34.           for (i = 0 ; i < 2 ; i++)
  35.                for (j = 0 ; j < NUM ; j++)
  36.                     {
  37.                     if (i == 0)
  38.                          {
  39.                          iKeep [j] [0] = x1 = cx * (rand () % 10) ;
  40.                          iKeep [j] [1] = y1 = cy * (rand () % 10) ;
  41.                          iKeep [j] [2] = x2 = cx * (rand () % 10) ;
  42.                          iKeep [j] [3] = y2 = cy * (rand () % 10) ;
  43.                          }
  44.                     else
  45.                          {
  46.                          x1 = iKeep [NUM - 1 - j] [0] ;
  47.                          y1 = iKeep [NUM - 1 - j] [1] ;
  48.                          x2 = iKeep [NUM - 1 - j] [2] ;
  49.                          y2 = iKeep [NUM - 1 - j] [3] ;
  50.                          }
  51.                     BitBlt (hdcMem, 0, 0, cx, cy, hdc,  x1, y1, SRCCOPY) ;
  52.                     BitBlt (hdc,  x1, y1, cx, cy, hdc,  x2, y2, SRCCOPY) ;
  53.                     BitBlt (hdc,  x2, y2, cx, cy, hdcMem, 0, 0, SRCCOPY) ;
  54.  
  55.                     Sleep (10) ;
  56.                     }
  57.  
  58.           DeleteDC (hdcMem) ;
  59.           DeleteDC (hdc) ;
  60.           DeleteObject (hBitmap) ;
  61.  
  62.           LockWindowUpdate (NULL) ;
  63.           }
  64.  
  65.      return FALSE ;
  66.      }
  67.