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