Click here to Skip to main content
12,456,455 members (56,573 online)
Click here to Skip to main content
Add your own
alternative version

Stats

134.6K views
811 downloads
40 bookmarked
Posted

CreateGrayscaleIcon

, 20 May 2002
Rate this:
Please Sign up or sign in to vote.
A Win32 function to create grayscale icons
<!-- Download Links --> <!-- Article image -->

Sample Image - CreateGrayscaleIcon.png

Abstract

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(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

History

  • 03/May/2002
    Removed dependancy from m_hWnd
    Removed 1 BitBlt operation
  • 16/April/2002
    First release

Disclaimer

THE SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO RESPONSIBILITIES FOR POSSIBLE DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.

Terms of use

THIS SOFTWARE IS FREE FOR PERSONAL USE OR FREEWARE APPLICATIONS.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Davide Calabro
Web Developer
Italy Italy
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralSmall bugs Pin
tHeWiZaRdOfDoS31-Oct-06 12:45
membertHeWiZaRdOfDoS31-Oct-06 12:45 
QuestionFail to use this function Pin
binglan14-Aug-06 21:16
memberbinglan14-Aug-06 21:16 
Generalsmall question... Pin
Max Santos7-Oct-04 6:54
memberMax Santos7-Oct-04 6:54 
GeneralRe: small question... Pin
Davide Calabro7-Oct-04 21:02
memberDavide Calabro7-Oct-04 21:02 
GeneralBug Pin
Michael Kaufmann21-Sep-04 2:57
memberMichael Kaufmann21-Sep-04 2:57 
GeneralIy's just fit me. thanks a lot. Pin
下眩月26-Sep-03 16:45
member下眩月26-Sep-03 16:45 
GeneralGood work Pin
jawad ayub baig22-May-02 20:05
memberjawad ayub baig22-May-02 20:05 
GeneralVery Usefully Thanks Pin
Anonymous27-Apr-02 17:31
memberAnonymous27-Apr-02 17:31 
GeneralSemi Related Question Pin
Norm Almond24-Apr-02 22:18
memberNorm Almond24-Apr-02 22:18 
GeneralRe: Semi Related Question Pin
Davide Calabro25-Apr-02 3:49
memberDavide Calabro25-Apr-02 3:49 
GeneralRe: Semi Related Question Pin
Emmanuel Deloget29-Apr-02 21:41
memberEmmanuel Deloget29-Apr-02 21:41 
GeneralUseful. Pin
Shog924-Apr-02 9:41
memberShog924-Apr-02 9:41 
GeneralRe: Useful. Pin
Davide Calabro24-Apr-02 10:36
memberDavide Calabro24-Apr-02 10:36 
QuestionHey, why you focus only on m_hWnd ? Pin
Davide Calabro24-Apr-02 3:58
memberDavide Calabro24-Apr-02 3:58 
AnswerRe: Hey, why you focus only on m_hWnd ? Pin
Ed Gadziemski24-Apr-02 8:27
memberEd Gadziemski24-Apr-02 8:27 
GeneralRe: Hey, why you focus only on m_hWnd ? Pin
Davide Calabro24-Apr-02 10:38
memberDavide Calabro24-Apr-02 10:38 
AnswerRe: Hey, why you focus only on m_hWnd ? Pin
Ed Gadziemski24-Apr-02 8:33
memberEd Gadziemski24-Apr-02 8:33 
GeneralRe: Hey, why you focus only on m_hWnd ? Pin
Davide Calabro24-Apr-02 10:38
memberDavide Calabro24-Apr-02 10:38 
AnswerSorry Davide Pin
Jean-Michel LE FOL24-Apr-02 21:15
memberJean-Michel LE FOL24-Apr-02 21:15 
GeneralRe: Sorry Davide Pin
Davide Calabro25-Apr-02 3:46
memberDavide Calabro25-Apr-02 3:46 
Questionand m_hWnd, is it pure Win32 Application ??? Pin
Jean-Michel LE FOL23-Apr-02 22:33
memberJean-Michel LE FOL23-Apr-02 22:33 
AnswerRe: and m_hWnd, is it pure Win32 Application ??? Pin
Robert W.24-Apr-02 0:45
memberRobert W.24-Apr-02 0:45 
GeneralRe: and m_hWnd, is it pure Win32 Application ??? Pin
Robert W.24-Apr-02 0:47
memberRobert W.24-Apr-02 0:47 
GeneralRe: and m_hWnd, is it pure Win32 Application ??? Pin
Maximilian Hänel24-Apr-02 1:11
memberMaximilian Hänel24-Apr-02 1:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160826.1 | Last Updated 21 May 2002
Article Copyright 2002 by Davide Calabro
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid