Microsoft KB Archive/128786

From BetaArchive Wiki

HOWTO: How to Shade Images to Look Like Windows 95 Active Icon

Q128786



The information in this article applies to:


  • Microsoft Win32 Application Programming Interface (API), used with:
    • Microsoft Windows NT Server versions 3.51, 4.0
    • Microsoft Windows NT Workstation versions 3.51, 4.0
    • Microsoft Windows 95
  • Microsoft Windows 2000 Advanced Server
  • Microsoft Windows 2000 Server
  • Microsoft Windows 2000 Professional





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 query words: 4.00 hatch darken shadow

Keywords : kbOSWinNT351 kbOSWinNT400 kbOSWin2000 kbSDKWin32 kbDSupport kbGDIFAQ
Issue type : kbhowto
Technology : kbAudDeveloper kbWin32sSearch kbWin32API


Last Reviewed: December 16, 2000
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.