【发布时间】:2011-01-22 07:06:53
【问题描述】:
我正在尝试使用像素缓冲区创建与 Windows 兼容的图标。 Surface 类加载图像并在内部将其保存为无符号整数数组 (0xRRGGBB)。
我正在尝试像这样创建一个图标:
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
);
但是,result 始终是 NULL。我在任何地方都找不到CreateIconFromResourceEx 期望的数据格式。
最终我想从外部文件加载图标,而不使用资源文件。
提前致谢。
编辑:我想通了!最终编码:
// 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);
【问题讨论】:
-
尽可能避免使用 SetPixel。这太慢了。尝试使用您的原始字节作为源创建一个 DIB 部分(如果您正确设置了 BITMAPHEADER,Windows 将直接采用您的像素格式)。
-
没关系,这都是在初始化时完成的。