How to Shade Images to Look Like Windows 95 Active Icon

Last reviewed: September 29, 1995
Article ID: Q128786
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

This article shows by example how to display an image or an icon in a shaded state, as Windows 95 does for the active icon.

MORE INFORMATION

Step-by-Step Procedure

To obtain the shaded look for your image or icon, follow these six steps:

  1. Create a compatible DC and bitmap.

  2. Create a monochrome pattern brush with every other pixel on.

  3. Fill the memory image with the pattern.

  4. BitBlt the source image over the pattern using SRCAND so that only the
       'on' destination pixels are transferred.
    
    

  5. Color the destination with the pattern, using the highlight color for the 'off' pixels and using black for the 'on' pixels.

  6. Copy the filtered original from the memory DC to the destination using SRCPAINT so that only the 'on' pixels are transferred.

This results in the destination having the original image with every other pixel colored with the highlight color.

Sample Code

The following function implements these six steps to shade a rectangular area on a device context:

// ShadeRect
// hDC    : the DC on which the area is to be shaded
// lpRect : the coordinates within which to shade
BOOL ShadeRect( HDC hDC, LPRECT lpRect ) {
   COLORREF  crHighlightColor, crOldBkColor, crOldTextColor;
   HBRUSH    hBrush, hOldBrush;
   HBITMAP   hBitmap, hBrushBitmap, hOldMemBitmap;
   int       OldBkMode, nWidth, nHeight;
   HDC       hMemDC;
   RECT      rcRect = { 0, 0, 0, 0};
   // The bitmap bits are for a monochrome "every-other-pixel"
   //     bitmap (for a pattern brush)
   WORD      Bits[8] = { 0x0055, 0x00aa, 0x0055, 0x00aa,
                         0x0055, 0x00aa, 0x0055, 0x00aa };

   // The Width and Height of the target area
   nWidth = lpRect->right - lpRect->left + 1;
   nHeight = lpRect->bottom - lpRect->top + 1;

   // Need a pattern bitmap
   hBrushBitmap = CreateBitmap( 8, 8, 1, 1, &Bits );
   // Need to store the original image
   hBitmap = CreateCompatibleBitmap( hDC, nWidth, nHeight );
   // Need a memory DC to work in
   hMemDC = CreateCompatibleDC( hDC );
   // Create the pattern brush
   hBrush = CreatePatternBrush( hBrushBitmap );

   // Has anything failed so far? If so, abort!
   if( (hBrushBitmap==NULL) || (hBitmap==NULL) ||
       (hMemDC==NULL) || (hBrush==NULL) )
   {
      if( hBrushBitmap != NULL ) DeleteObject(hBrushBitmap);
      if( hBitmap != NULL ) DeleteObject( hBitmap );
      if( hMemDC != NULL ) DeleteDC( hMemDC );
      if( hBrush != NULL ) DeleteObject( hBrush );
      return FALSE;
   }

   // Select the bitmap into the memory DC
   hOldMemBitmap = SelectObject( hMemDC, hBitmap );

   // How wide/tall is the original?
   rcRect.right = nWidth;
   rcRect.bottom = nHeight;

   // Lay down the pattern in the memory DC
   FillRect( hMemDC, &rcRect, hBrush );

   // Fill in the non-color pixels with the original image
   BitBlt( hMemDC, 0, 0, nWidth, nHeight, hDC,
   lpRect->left, lpRect->top, SRCAND );

   // For the "Shutdown" look, use black or gray here instead
   crHighlightColor = GetSysColor( COLOR_HIGHLIGHT );

   // Set the color scheme
   crOldTextColor = SetTextColor( hDC, crHighlightColor );
   crOldBkColor = SetBkColor( hDC, RGB(0,0,0) );
   SetBkMode( hDC, OPAQUE );

   // Select the pattern brush
   hOldBrush = SelectObject( hDC, hBrush );
   // Fill in the color pixels, and set the others to black
   FillRect( hDC, lpRect, hBrush );
   // Fill in the black ones with the original image
   BitBlt( hDC, lpRect->left, lpRect->top, nWidth, nHeight,
           hMemDC, 0, 0, SRCPAINT );

   // Restore target DC settings
   SetBkMode( hDC, OldBkMode );
   SetBkColor( hDC, crOldBkColor );
   SetTextColor( hDC, crOldTextColor );

   // Clean up
   SelectObject( hMemDC, hOldMemBitmap );
   DeleteObject( hBitmap );
   DeleteDC( hMemDC );
   DeleteObject( hBrushBitmap );
   SelectObject( hDC, hOldBrush );
   DeleteObject( hBrush );

   return TRUE;
}


Additional reference words: 4.00 hatch darken shadow
KBCategory: kbgraphic kbcode
KBSubcategory: GdiMisc


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.