Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to create a Windows-compatible icon using a pixel buffer. The Surface class loads an image and saves it as an unsigned int array internally (0xRRGGBB).

I'm trying to create an icon like so:

Surface m_Test("Data/Interface/CursorTest.png");
HICON result = CreateIconFromResourceEx( (PBYTE)m_Test.GetBuffer(), 
                                         m_Test.GetWidth() * m_Test.GetHeight(), 
                                         FALSE, 
                                         0, 
                                         24, 
                                         24, 
                                         LR_DEFAULTCOLOR
);

However, result is always NULL. I can't find anywhere what data format CreateIconFromResourceEx expects.

Ultimately I want to load an icon from an external file, without using resource files.

Thanks in advance.

EDIT: I figured it out! The final codez:

// m_Cursors is an array of images coming from an image strip
Pixel* buffer   = m_Cursors[i]->GetBuffer();
int width       = m_Cursors[i]->GetWidth();
int height      = m_Cursors[i]->GetHeight();

HDC dc_andmask = CreateCompatibleDC(dc);
HBITMAP cur_andmask = CreateCompatibleBitmap(dc, width, height);
HBITMAP hOldAndMaskBitmap = (HBITMAP)SelectObject(dc_andmask, cur_andmask);

HDC dc_xormask = CreateCompatibleDC(dc);
HBITMAP cur_xormask = CreateCompatibleBitmap(dc, width, height);
HBITMAP hOldXorMaskBitmap = (HBITMAP)SelectObject(dc_xormask, cur_xormask);

for (int y = 0; y < height; ++y)
{
    for (int x = 0; x < width; ++x)
    {
        Pixel currpix = buffer[x + y * width];

        // the images use the alpha channel for transparancy
        if ((currpix & 0xFF000000) == 0
        {
            // transparant
            SetPixel(dc_andmask, x, y, RGB(255, 255, 255));
            SetPixel(dc_xormask, x, y, RGB(0, 0, 0));
        }
        else
        {
            COLORREF curr = RGB(currpix & 0xFF0000 >> 16, currpix & 0x00FF00 >> 8, currpix & 0x0000FF);

            // opaque
            SetPixel(dc_andmask, x, y, RGB(0, 0, 0));
            SetPixel(dc_xormask, x, y, curr);
        }
    }
}

// i don't know why this has to be done, but it fixes things
// so who cares (b ")b
SelectObject(dc_andmask, hOldAndMaskBitmap);
SelectObject(dc_xormask, hOldXorMaskBitmap);

DeleteObject(dc_xormask);
DeleteObject(dc_andmask);

ICONINFO temp = { FALSE, m_OffsetX[i], m_OffsetY[i], cur_andmask, cur_xormask };
m_CursorIDs[i] = CreateIconIndirect(&temp);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
546 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use CreateIcon if you want to pass the raw bytes data from the AND and XOR mask.

If instead you want to be able to use HBITMAPs, you can use CreateIconIndirect. Using this API, you can can even create icons with an alpha channel if you so desire.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...