home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap14 / BitMask / BitMask.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.6 KB  |  141 lines

  1. /*-------------------------------------------
  2.    BITMASK.C -- Bitmap Masking Demonstration
  3.                 (c) Charles Petzold, 1998
  4.   -------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     PSTR szCmdLine, int iCmdShow)
  12. {
  13.      static TCHAR szAppName [] = TEXT ("BitMask") ;
  14.      HWND         hwnd ;
  15.      MSG          msg ;
  16.      WNDCLASS     wndclass ;
  17.  
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  19.      wndclass.lpfnWndProc   = WndProc ;
  20.      wndclass.cbClsExtra    = 0 ;
  21.      wndclass.cbWndExtra    = 0 ;
  22.      wndclass.hInstance     = hInstance ;
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  25.      wndclass.hbrBackground = (HBRUSH) GetStockObject (LTGRAY_BRUSH) ;
  26.      wndclass.lpszMenuName  = NULL ;
  27.      wndclass.lpszClassName = szAppName ;
  28.      
  29.      if (!RegisterClass (&wndclass))
  30.      {
  31.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  32.                       szAppName, MB_ICONERROR) ;
  33.           return 0 ;
  34.      }
  35.      
  36.      hwnd = CreateWindow (szAppName, TEXT ("Bitmap Masking Demo"), 
  37.                           WS_OVERLAPPEDWINDOW, 
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.  
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.  
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.      {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.      }
  50.      return msg.wParam ;
  51. }
  52.  
  53. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  54. {
  55.      static HBITMAP   hBitmapImag, hBitmapMask ;
  56.      static HINSTANCE hInstance ;
  57.      static int       cxClient, cyClient, cxBitmap, cyBitmap ;
  58.      BITMAP           bitmap ;
  59.      HDC              hdc, hdcMemImag, hdcMemMask ;
  60.      int              x, y ;
  61.      PAINTSTRUCT      ps ;
  62.      
  63.      switch (message)
  64.      {
  65.      case WM_CREATE:
  66.           hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
  67.  
  68.                // Load the original image and get its size
  69.  
  70.           hBitmapImag = LoadBitmap (hInstance, TEXT ("Matthew")) ;
  71.           GetObject (hBitmapImag, sizeof (BITMAP), &bitmap) ;
  72.           cxBitmap = bitmap.bmWidth ;
  73.           cyBitmap = bitmap.bmHeight ;
  74.  
  75.                // Select the original image into a memory DC
  76.  
  77.           hdcMemImag  = CreateCompatibleDC (NULL) ;
  78.           SelectObject (hdcMemImag, hBitmapImag) ;
  79.  
  80.                // Create the monochrome mask bitmap and memory DC
  81.  
  82.           hBitmapMask = CreateBitmap (cxBitmap, cyBitmap, 1, 1, NULL) ;
  83.           hdcMemMask = CreateCompatibleDC (NULL) ;
  84.           SelectObject (hdcMemMask, hBitmapMask) ;
  85.  
  86.                // Color the mask bitmap black with a white ellipse
  87.           
  88.           SelectObject (hdcMemMask, GetStockObject (BLACK_BRUSH)) ;
  89.           Rectangle (hdcMemMask, 0, 0, cxBitmap, cyBitmap) ;
  90.           SelectObject (hdcMemMask, GetStockObject (WHITE_BRUSH)) ;
  91.           Ellipse (hdcMemMask, 0, 0, cxBitmap, cyBitmap) ;
  92.  
  93.                // Mask the original image
  94.  
  95.           BitBlt (hdcMemImag, 0, 0, cxBitmap, cyBitmap, 
  96.                   hdcMemMask, 0, 0, SRCAND) ;
  97.  
  98.           DeleteDC (hdcMemImag) ;
  99.           DeleteDC (hdcMemMask) ;
  100.           return 0 ;
  101.  
  102.      case WM_SIZE:
  103.           cxClient = LOWORD (lParam) ;
  104.           cyClient = HIWORD (lParam) ;
  105.           return 0 ;
  106.  
  107.      case WM_PAINT:
  108.           hdc = BeginPaint (hwnd, &ps) ;
  109.  
  110.                // Select bitmaps into memory DCs
  111.  
  112.           hdcMemImag = CreateCompatibleDC (hdc) ;
  113.           SelectObject (hdcMemImag, hBitmapImag) ;
  114.  
  115.           hdcMemMask = CreateCompatibleDC (hdc) ;
  116.           SelectObject (hdcMemMask, hBitmapMask) ;
  117.  
  118.                // Center image
  119.  
  120.           x = (cxClient - cxBitmap) / 2 ;
  121.           y = (cyClient - cyBitmap) / 2 ;
  122.                
  123.                // Do the bitblts
  124.  
  125.           BitBlt (hdc, x, y, cxBitmap, cyBitmap, hdcMemMask, 0, 0, 0x220326) ;
  126.           BitBlt (hdc, x, y, cxBitmap, cyBitmap, hdcMemImag, 0, 0, SRCPAINT) ;
  127.  
  128.           DeleteDC (hdcMemImag) ;
  129.           DeleteDC (hdcMemMask) ;
  130.           EndPaint (hwnd, &ps) ;
  131.           return 0 ;
  132.  
  133.      case WM_DESTROY:
  134.           DeleteObject (hBitmapImag) ;
  135.           DeleteObject (hBitmapMask) ;
  136.           PostQuitMessage (0) ;
  137.           return 0 ;
  138.      }
  139.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  140. }
  141.