home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / activedocument / dfv / bitmaps.cxx < prev    next >
C/C++ Source or Header  |  1995-02-13  |  6KB  |  194 lines

  1. //+---------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //  Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. //  File:       bitmaps.cxx
  7. //
  8. //  Contents:   bitmap helper functions
  9. //
  10. //  Classes:
  11. //
  12. //  Functions:  DDBChangeColor
  13. //              LoadAndStretch
  14. //
  15. //  History:    6-24-94   stevebl   Created
  16. //
  17. //----------------------------------------------------------------------------
  18.  
  19. #include <windows.h>
  20. #include "bitmaps.h"
  21.  
  22. //+---------------------------------------------------------------------------
  23. //
  24. //  Function:   LoadAndStretch
  25. //
  26. //  Synopsis:   Loads a bitmap from our resources, and creates a bitmap
  27. //              "array."  The red portions of the bitmap are considered
  28. //              clear.  Each resource consists of two bitmaps side by
  29. //              side:  an "open folder" and a "closed folder".  These
  30. //              bitmaps are turned into 4 bitmaps:  two selected, and
  31. //              two unselected bitmaps.
  32. //
  33. //  Arguments:  [hinst]        - instance containing the bitmap
  34. //              [hbmpDest]     - destination bitmap array
  35. //              [lpszResource] - name of the bitmap resource
  36. //              [cxBitmap]     - width to stretch to
  37. //              [cyBitmap]     - height to stretch to
  38. //              [crHigh]       - color of the highlighted background
  39. //              [crNorm]       - color of the normal background
  40. //
  41. //  Returns:    TRUE on success.
  42. //              FALSE on failure.
  43. //
  44. //  History:    6-24-94   stevebl   Stolen and modified from original DFView
  45. //
  46. //----------------------------------------------------------------------------
  47.  
  48. BOOL LoadAndStretch (
  49.     HINSTANCE hinst,
  50.     HBITMAP hbmpDest,
  51.     LPTSTR lpszResource,
  52.     UINT cxBitmap,
  53.     UINT cyBitmap,
  54.     COLORREF crHigh,
  55.     COLORREF crNorm)
  56. {
  57.     HBITMAP hbmpOld1, hbmpOld2, hbmp;
  58.     BITMAP bm;
  59.     int i;
  60.     HDC hdcDest = CreateCompatibleDC(NULL);
  61.     HDC hdcSrc = CreateCompatibleDC(NULL);
  62.  
  63.     if (NULL == hdcDest || NULL == hdcSrc)
  64.     {
  65.         DeleteDC(hdcSrc);
  66.         DeleteDC(hdcDest);
  67.         return(FALSE);
  68.     }
  69.     for (i=0;  i < 2;  i++)
  70.     {
  71.         hbmp = LoadBitmap(hinst, lpszResource);
  72.         if (NULL == hbmp)
  73.         {
  74.             DeleteDC(hdcSrc);
  75.             DeleteDC(hdcDest);
  76.             return FALSE;
  77.         }
  78.  
  79.         DDBChangeColor(hbmp, RGB (255,0,0), ((i == 0) ? crHigh : crNorm));
  80.         GetObject(hbmp, sizeof (bm), &bm);
  81.  
  82.         hbmpOld1 = (HBITMAP) SelectObject(hdcDest, hbmpDest);
  83.         hbmpOld2 = (HBITMAP) SelectObject(hdcSrc, hbmp);
  84.  
  85.         SetStretchBltMode(hdcDest, COLORONCOLOR);
  86.         StretchBlt(
  87.             hdcDest,
  88.             i * cxBitmap * 2,
  89.             0,
  90.             cxBitmap * 2,
  91.             cyBitmap,
  92.             hdcSrc,
  93.             0,
  94.             0,
  95.             bm.bmWidth,
  96.             bm.bmHeight,
  97.             SRCCOPY);
  98.  
  99.         SelectObject(hdcDest, hbmpOld1);
  100.         SelectObject(hdcSrc,  hbmpOld2);
  101.         DeleteObject(hbmp);
  102.     }
  103.  
  104.     DeleteDC(hdcSrc);
  105.     DeleteDC(hdcDest);
  106.     return TRUE;
  107. }
  108.  
  109.  
  110.  
  111. //+---------------------------------------------------------------------------
  112. //
  113. //  Function:   DDBChangeColor
  114. //
  115. //  Synopsis:   Change a particular color in a bitmap to another color.
  116. //              Strategy is to create a monochrome mask, where each pixel
  117. //              in the source bitmap that matches the color we're converting
  118. //              from is set to white (1), and all other colors to black.
  119. //              We then Blt this mask into the original bitmap, with a ROP
  120. //              code that does:
  121. //
  122. //                (~Mask&Source) | (~Mask&Pattern)
  123. //
  124. //              where Pattern is the color we're changing to.
  125. //
  126. //              In other words, wherever the Mask is 1, we want to put the pattern;
  127. //              wherever the Mask is 0, we want to leave the source alone.  By
  128. //              using a Truth Table, you'll find that this ROP code is equivalent
  129. //              to DSPDxax or ROP code 0x00E20746.
  130. //
  131. //              For info on figuring out ROP codes given a set of boolean ops,
  132. //              check out the Windows 3.0 SDK, Reference Volume 2, chapter 11.
  133. //
  134. //  Arguments:  [hBitmap] - bitmap handle
  135. //              [crFrom]  - color to change
  136. //              [crTo]    - new color
  137. //
  138. //  Returns:    TRUE on success.  FALSE on failur
  139. //
  140. //  History:    6-24-94   stevebl   Stolen from original DFView code
  141. //
  142. //----------------------------------------------------------------------------
  143.  
  144. BOOL DDBChangeColor (HBITMAP hBitmap, COLORREF crFrom, COLORREF crTo)
  145. {
  146.     register int cx, cy;
  147.     BITMAP       bm;
  148.     HDC          hdcBmp, hdcMask;
  149.     HBITMAP      hbmMask, hbmOld1, hbmOld2;
  150.     HBRUSH       hBrush, hbrOld;
  151.  
  152.     if (!hBitmap)
  153.           return FALSE;
  154.  
  155.     GetObject (hBitmap, sizeof (bm), &bm);
  156.     cx = bm.bmWidth;
  157.     cy = bm.bmHeight;
  158.  
  159.     hbmMask = CreateBitmap(cx, cy, 1, 1, NULL);
  160.     hdcMask = CreateCompatibleDC(NULL);
  161.     hdcBmp = CreateCompatibleDC(NULL);
  162.     hBrush = CreateSolidBrush(crTo);
  163.  
  164.     if (!hdcMask || !hdcBmp || !hBrush || !hbmMask)
  165.     {
  166.         DeleteObject(hbmMask);
  167.         DeleteObject(hBrush);
  168.         DeleteDC(hdcMask);
  169.         DeleteDC(hdcBmp);
  170.         return FALSE;
  171.     }
  172.  
  173.     hbmOld1 = (HBITMAP) SelectObject (hdcBmp,  hBitmap);
  174.     hbmOld2 = (HBITMAP) SelectObject (hdcMask, hbmMask);
  175.     hbrOld  = (HBRUSH) SelectObject (hdcBmp, hBrush);
  176.  
  177.     SetBkColor(hdcBmp, crFrom);
  178.     BitBlt(hdcMask, 0, 0, cx, cy, hdcBmp,  0, 0, SRCCOPY);
  179.     SetBkColor(hdcBmp, RGB(255,255,255));
  180.     BitBlt(hdcBmp,  0, 0, cx, cy, hdcMask, 0, 0, DSPDxax);
  181.  
  182.     SelectObject(hdcBmp,  hbrOld);
  183.     SelectObject(hdcBmp,  hbmOld1);
  184.     SelectObject(hdcMask, hbmOld2);
  185.     DeleteDC(hdcBmp);
  186.     DeleteDC(hdcMask);
  187.     DeleteObject(hBrush);
  188.     DeleteObject(hbmMask);
  189.  
  190.     return TRUE;
  191. }
  192.  
  193.  
  194.