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), &
amp;bmp))
{
csGrayII.hbmColor =
::CreateBitmap(csII.xHotspot*2,
csII.yHotspot*2,
bmp.bmPlanes,
bmp.bmBitsPixel,
NULL);
if (csGrayII.hbmColor)
{
hOldBmp1 =
(HBITMAP)::SelectObject(hMemDC1,
csII.hbmColor);
hOldBmp2 =
(HBITMAP)::SelectObject(hMemDC2,
csGrayII.hbmColor);
::BitBlt(hMemDC2, 0, 0, csII.xHotspot*2,
csII.yHotspot*2, hMemDC1, 0, 0,
SRCCOPY);
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,
dwLoopX,
dwLoopY,
RGB(byNewPixel,
byNewPixel,
byNewPixel));
} // for
} // for
::SelectObject(hMemDC1, hOldBmp1);
::SelectObject(hMemDC2, hOldBmp2);
csGrayII.hbmMask = csII.hbmMask;
csGrayII.fIcon = TRUE;
hGrayIcon = ::CreateIconIndirect(&#38;csGrayII);
} // if
::DeleteObject(csGrayII.hbmColor);
//::DeleteObject(csGrayII.hbmMask);
} // if
::DeleteObject(csII.hbmColor);
::DeleteObject(csII.hbmMask);
::DeleteDC(hMemDC1);
::DeleteDC(hMemDC2);
::ReleaseDC(m_hWnd, hMainDC);
return hGrayIcon;
} |