miniicon.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 9k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /*
  2. **------------------------------------------------------------------------------
  3. ** Module:  Disk Cleanup Applet
  4. ** File:    miniicon.c
  5. **
  6. ** Purpose: Creates and Manages the mini icons
  7. ** Notes:   
  8. ** Mod Log: Created by Jason Cobb (2/97)
  9. **
  10. ** Copyright (c)1997 Microsoft Corporation, All Rights Reserved
  11. **------------------------------------------------------------------------------
  12. */
  13. /*
  14. **------------------------------------------------------------------------------
  15. ** Project include files
  16. **------------------------------------------------------------------------------
  17. */
  18. #include "common.h"
  19. #include "resource.h"
  20. #include "miniicon.h"
  21. /*
  22. **------------------------------------------------------------------------------
  23. ** Local variables
  24. **------------------------------------------------------------------------------
  25. */
  26. HINSTANCE   MiniIcon::hInstance             = NULL;
  27. MiniIcon::MiniIcon(
  28.     void
  29.     )
  30. {
  31.     hdcMiniMem = NULL;
  32.     hbmMiniImage = NULL;
  33.     hbmMiniMask = NULL;
  34.     NumImages = 0;
  35.  
  36.     CreateMiniIcons();
  37. }
  38. MiniIcon::~MiniIcon(
  39.     void
  40.     )
  41. {
  42.     DestroyMiniIcons();
  43. }
  44. void
  45. MiniIcon::Register(
  46.     HINSTANCE hInstance
  47.     )
  48. {
  49.     MiniIcon::hInstance = hInstance;
  50. }
  51. void
  52. MiniIcon::Unregister(
  53.     void
  54.     )
  55. {
  56.     MiniIcon::hInstance= NULL;
  57. }
  58. BOOL
  59. MiniIcon::CreateMiniIcons(
  60.     void
  61.     )
  62. {
  63.     HDC     hdc, hdcMem;
  64.     HBITMAP hbmOld;
  65.     BITMAP  bm;
  66.     BOOL    bRet = FALSE;          // assume failure
  67.     MiDebugMsg((0, "MiniIcon:CreateMiniIcons()"));
  68.     if(hdcMiniMem) 
  69.     {
  70.         //
  71.         // Then the mini-icon list has already been built, so
  72.         // return success.
  73.         //
  74.         return TRUE;
  75.     }
  76.     hdc = GetDC(NULL);
  77.     hdcMiniMem = CreateCompatibleDC(hdc);
  78.     ReleaseDC(NULL, hdc);
  79.     if(!hdcMiniMem) 
  80.     {
  81.         goto clean0;
  82.     }
  83.     if(!(hdcMem = CreateCompatibleDC(hdcMiniMem))) 
  84.     {
  85.         goto clean0;
  86.     }
  87.     if(!(hbmMiniImage = LoadBitmap(MiniIcon::hInstance, MAKEINTRESOURCE(BMP_MINIICONS)))) 
  88.     {
  89.         goto clean1;
  90.     }
  91.     GetObject(hbmMiniImage, sizeof(bm), &bm);
  92.     if(!(hbmMiniMask = CreateBitmap(bm.bmWidth,
  93.                                     bm.bmHeight,
  94.                                     1,
  95.                                     1,
  96.                                     NULL))) 
  97.     {
  98.         goto clean1;
  99.     }
  100.     hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMiniImage);
  101.     SelectObject(hdcMiniMem, hbmMiniMask);
  102.     //
  103.     // make the mask.  white where transparent, black where opaque
  104.     //
  105.     SetBkColor(hdcMem, RGB_TRANSPARENT);
  106.     BitBlt(hdcMiniMem,
  107.            0,
  108.            0,
  109.            bm.bmWidth,
  110.            bm.bmHeight,
  111.            hdcMem,
  112.            0,
  113.            0,
  114.            SRCCOPY
  115.           );
  116.     //
  117.     // black-out all of the transparent parts of the image, in preparation
  118.     // for drawing.
  119.     //
  120.     SetBkColor(hdcMem, RGB_BLACK);
  121.     SetTextColor(hdcMem, RGB_WHITE);
  122.     BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMiniMem, 0, 0, SRCAND);
  123.     SelectObject(hdcMiniMem, hbmOld);
  124.     NumImages = bm.bmWidth/MINIX;
  125.     bRet = TRUE;
  126.     SelectObject(hdcMem, hbmOld);
  127. clean1:
  128.     DeleteObject(hdcMem);
  129. clean0:
  130.     //
  131.     // If failure, clean up anything we might have built
  132.     //
  133.     if(!bRet) 
  134.     {
  135.         DestroyMiniIcons();
  136.     }
  137.     return bRet;
  138. }
  139. void
  140. MiniIcon::DestroyMiniIcons(
  141.     void
  142.     )
  143. {
  144.     MiDebugMsg((0, "MiniIcon::DestroyMiniIcons()"));
  145.     if(hdcMiniMem) 
  146.     {
  147.         DeleteDC(hdcMiniMem);
  148.         hdcMiniMem = NULL;
  149.     }
  150.     if(hbmMiniImage) 
  151.     {
  152.         DeleteObject(hbmMiniImage);
  153.         hbmMiniImage = NULL;
  154.     }
  155.     if(hbmMiniMask) 
  156.     {
  157.         DeleteObject(hbmMiniMask);
  158.         hbmMiniMask = NULL;
  159.     }
  160.     NumImages = 0;
  161. }
  162. /**************************************************************************
  163. Routine Description:
  164.     This routine draws the specified mini-icon at the requested location.
  165. Arguments:
  166.     hdc - Supplies the handle of the device context in which the mini-icon
  167.         will be drawn.
  168.     rc - Supplies the rectangle in the specified HDC to draw the icon in.
  169.     MiniIconIndex - The index of the mini-icon
  170.             0   Check
  171.             1   Uncheck
  172.     Flags - Controls the drawing operation.  The LOWORD contains the actual flags
  173.         defined as follows:
  174.         DMI_MASK - Draw the mini-icon's mask into HDC.
  175.         DMI_BKCOLOR - Use the system color index specified in the HIWORD of Flags
  176.             as the background color.  If not specified, COLOR_WINDOW is used.
  177.         DMI_USERECT - If set, DrawMiniIcon will use the supplied rect,
  178.             stretching the icon to fit as appropriate.
  179. Return Value:
  180.     This function returns the offset from the left of rc where the string should
  181.     start.
  182. Remarks:
  183.     By default, the icon will be centered vertically and butted against the left
  184.     corner of the specified rectangle.
  185. **********************************************************************************/
  186. int
  187. MiniIcon::DrawMiniIcon(
  188.     HDC   hdc,
  189.     RECT  rc,
  190.     INT   MiniIconIndex,
  191.     DWORD Flags
  192.     )
  193. {
  194.     HBITMAP hbmOld;
  195.     DWORD rgbBk, rgbText;
  196.     INT ret = 0;
  197.     if(hbmMiniImage) 
  198.     {
  199.         //
  200.         // Set the Foreground and  background color for the
  201.         // conversion of the Mono Mask image
  202.         //
  203.         if(Flags & DMI_MASK) 
  204.         {
  205.             rgbBk = SetBkColor(hdc, RGB_WHITE);
  206.         } 
  207.         
  208.         else 
  209.         {
  210.             rgbBk = SetBkColor(hdc,
  211.                                GetSysColor(((int)(Flags & DMI_BKCOLOR
  212.                                                       ? HIWORD(Flags)
  213.                                                       : COLOR_WINDOW)))
  214.                               );
  215.         }
  216.         
  217.         rgbText = SetTextColor(hdc, RGB_BLACK);
  218.         if(Flags & DMI_USERECT) 
  219.         {
  220.             //
  221.             // Copy the converted mask into the dest.  The transparent
  222.             // areas will be drawn with the current window color.
  223.             //
  224.             hbmOld = (HBITMAP)SelectObject(hdcMiniMem,
  225.                                   hbmMiniMask
  226.                                  );
  227.             StretchBlt(hdc,
  228.                        rc.left,
  229.                        rc.top,
  230.                        rc.right - rc.left,
  231.                        rc.bottom - rc.top,
  232.                        hdcMiniMem,
  233.                        MINIX * MiniIconIndex,
  234.                        0,
  235.                        MINIX,
  236.                        MINIY,
  237.                        SRCCOPY);
  238.             if(!(Flags & DMI_MASK)) 
  239.             {
  240.                 //
  241.                 // OR the image into the dest
  242.                 //
  243.                 SelectObject(hdcMiniMem,
  244.                              hbmMiniImage
  245.                             );
  246.                             
  247.                 StretchBlt(hdc,
  248.                            rc.left,
  249.                            rc.top,
  250.                            rc.right - rc.left,
  251.                            rc.bottom - rc.top,
  252.                            hdcMiniMem,
  253.                            MINIX * MiniIconIndex,
  254.                            0,
  255.                            MINIX,
  256.                            MINIY,
  257.                            SRCPAINT);
  258.             }
  259.         } 
  260.         
  261.         else 
  262.         {
  263.             //
  264.             // Copy the converted mask into the dest.  The transparent
  265.             // areas will be drawn with the current window color.
  266.             //
  267.             hbmOld = (HBITMAP)SelectObject(hdcMiniMem,
  268.                                   hbmMiniMask
  269.                                  );
  270.             
  271.             BitBlt(hdc,
  272.                    rc.left,
  273.                    rc.top + (rc.bottom - rc.top - MINIY)/2,
  274.                    MINIX,
  275.                    MINIY,
  276.                    hdcMiniMem,
  277.                    MINIX * MiniIconIndex,
  278.                    0,
  279.                    SRCCOPY
  280.                   );
  281.             if(!(Flags & DMI_MASK)) 
  282.             {
  283.                 //
  284.                 // OR the image into the dest
  285.                 //
  286.                 SelectObject(hdcMiniMem,
  287.                              hbmMiniImage
  288.                             );
  289.                 BitBlt(hdc,
  290.                        rc.left,
  291.                        rc.top + (rc.bottom - rc.top - MINIY)/2,
  292.                        MINIX,
  293.                        MINIY,
  294.                        hdcMiniMem,
  295.                        MINIX * MiniIconIndex,
  296.                        0,
  297.                        SRCPAINT
  298.                       );
  299.             }
  300.         }
  301.         SetBkColor(hdc, rgbBk);
  302.         SetTextColor(hdc, rgbText);
  303.         SelectObject(hdcMiniMem, hbmOld);
  304.         if(Flags & DMI_USERECT) 
  305.         {
  306.             ret = rc.right - rc.left + 2;   // offset to string from left edge
  307.         } 
  308.         
  309.         else 
  310.         {
  311.             ret = MINIX + 2;                // offset to string from left edge
  312.         }
  313.     }
  314.     return ret;
  315. }