65.9K
CodeProject 正在变化。 阅读更多。
Home

CreateGrayscaleIcon

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.28/5 (8投票s)

2002年4月24日

viewsIcon

356850

downloadIcon

903

一个用于创建灰度图标的 Win32 函数。

Sample Image - CreateGrayscaleIcon.png

摘要

CreateGrayscaleIcon 是一个 Win32 函数,它从给定的图标开始创建一个灰度图标。
生成的图标将与原始图标具有相同的大小。 此函数仅使用 Win32 API 编写
以便能够在 MFC 和纯 Win32 应用程序中使用。

函数描述

只需将类似以下的函数添加到您的项目中

// 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(NULL);
    hMemDC1 = ::CreateCompatibleDC(hMainDC);
    hMemDC2 = ::CreateCompatibleDC(hMainDC);
    if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
  
    if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
    {
        DWORD   dwWidth = csII.xHotspot*2;
        DWORD   dwHeight = csII.yHotspot*2;

        csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, 
                                           bmp.bmBitsPixel, NULL);
        if (csGrayII.hbmColor)
        {
            hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
            hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);

            //::BitBlt(hMemDC2, 0, 0, dwWidth, dwHeight, hMemDC1, 0, 0, 
            //         SRCCOPY);

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

            for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
            {
                for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
                {
                    crPixel = ::GetPixel(hMemDC1, 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(&csGrayII);
        } // if

        ::DeleteObject(csGrayII.hbmColor);
        //::DeleteObject(csGrayII.hbmMask);
    } // if

    ::DeleteObject(csII.hbmColor);
    ::DeleteObject(csII.hbmMask);
    ::DeleteDC(hMemDC1);
    ::DeleteDC(hMemDC2);
    ::ReleaseDC(NULL, hMainDC);

    return hGrayIcon;
} // End of CreateGrayscaleIcon

历史

  • 2002/05/03
    移除了对 m_hWnd 的依赖
    移除了一个 BitBlt 操作
  • 2002/04/16
    首次发布

免责声明

软件和随附的文件按“原样”分发,不提供任何明示或暗示的保证。对于可能造成的任何损害或功能,概不负责。用户必须承担使用此软件的全部风险。

使用条款

此软件可免费用于个人用途或免费软件应用程序。

© . All rights reserved.