Sample Image

SoftechSoftware homepage
SoftechSoftware Email

Environment: VC++ 6.0, XP, Win2k, NT 4.0, Win9x/ME


CreateGrayscaleIcon is a Win32 function that creates a grayscale icon starting from a given icon. The resulting icon will have the same size of the original one. This function is written using only Win32 APIs to make possible to be used either in MFC and pure Win32 applications.

Function description

Just add in your project a function like the following:

// This function creates a grayscale icon starting
// from a given icon. The resulting icon will have
// the same size of the original one.
// Parameters:
//      [IN]    hIcon
//              Handle to the original icon.
// Return value:
//      If the function succeeds, the return value is 
//      the handle to the newly created grayscale icon.
//      If the function fails, the return value is NULL.
HICON CreateGrayscaleIcon(HICON hIcon)
  HICON       hGrayIcon = NULL;
  HDC         hMainDC = NULL, 
              hMemDC1 = NULL, 
              hMemDC2 = NULL;
  BITMAP      bmp;
  HBITMAP     hOldBmp1 = NULL,
              hOldBmp2 = NULL;
  ICONINFO    csII, csGrayII;
  BOOL        bRetValue = FALSE;

  bRetValue = ::GetIconInfo(hIcon, &csII);
  if (bRetValue == FALSE) return NULL;

  hMainDC = ::GetDC(m_hWnd);
  hMemDC1 = ::CreateCompatibleDC(hMainDC);
  hMemDC2 = ::CreateCompatibleDC(hMainDC);
  if (hMainDC == NULL || 
    hMemDC1 == NULL ||
    hMemDC2 == NULL) 
      return NULL;

  if (::GetObject(csII.hbmColor, 
                sizeof(BITMAP), &
    csGrayII.hbmColor = 
    if (csGrayII.hbmColor)
      hOldBmp1 =
      hOldBmp2 = 

      ::BitBlt(hMemDC2, 0, 0, csII.xHotspot*2,
               csII.yHotspot*2, hMemDC1, 0, 0,

      DWORD    dwLoopY = 0, dwLoopX = 0;
      COLORREF crPixel = 0;
      BYTE     byNewPixel = 0;

      for (dwLoopY = 0; dwLoopY < csII.yHotspot*2; dwLoopY++)
        for (dwLoopX = 0; dwLoopX < csII.xHotspot*2; dwLoopX++)
          crPixel = ::GetPixel(hMemDC2, dwLoopX, dwLoopY);

          byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) +
               (GetGValue(crPixel) * 0.587) +
               (GetBValue(crPixel) * 0.114));
          if (crPixel) ::SetPixel(hMemDC2,
        } // for
      } // for

      ::SelectObject(hMemDC1, hOldBmp1);
      ::SelectObject(hMemDC2, hOldBmp2);

      csGrayII.hbmMask = csII.hbmMask;

      csGrayII.fIcon = TRUE;
      hGrayIcon = ::CreateIconIndirect(&csGrayII);
    } // if

  } // if

  ::ReleaseDC(m_hWnd, hMainDC);

  return hGrayIcon;
} // End of CreateGrayscaleIcon

  • Date Published: April 23, 2002
    First release (4/16)


Download source code & demo project - 123 Kb


Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • One of the most foolproof ways for an online system to confirm, "Is it really you?" is by adding two-factor authentication. Modern two-factor solutions have evolved to support new, complex technology models that change how we use data, including cloud computing and bring-your-own-device (BYOD). However, not all two-factor authentication solutions are created equal. This guide walks through some of the key area of differentiation between two-factor authentication solutions and provides some concrete criteria …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds