我已经有很长时间没有处理 Win32 菜单了。我需要将一些 PNG 图标添加到 Win32 上下文弹出菜单中。自然地,我想在此过程中保留 PNG 透明度和所有每像素 alpha。这可能吗?
我正在考虑使用 SetMenuItemBitmaps
。这是要走的路吗?
我将我的 PNG 作为“PNG”资源导入,但我似乎无法使用 LoadBitmap
或 LoadImage
加载它们。我找到了一些关于使用 Gdi+
的建议,但显然我不会绘制菜单 - 系统会。
似乎有一种方法可以从 Gdi+ Bitmap
获取 HBITMAP
,但看起来好像所有的 alpha 都在这个过程中丢失了。 AFAIK,HBITMAP
可以愉快地承载 alpha 信息。
最佳答案
您需要 GDI+ 来加载 PNG。然后您需要创建一个正确大小的 32 位 alpha 位图,在该位图上创建一个 Graphics,并使用 DrawImage 将 PNG 复制到位图。这会为您提供带有 alpha channel 的位图。
像这样:
Image* pimgSrc = Image::FromFile("MyIcon.png"L, FALSE);
Bitmap* pbmpImage = new Bitmap(
iWidth, iHeight,
PixelFormat32bppARGB
);
Graphics* pgraphics = Graphics::FromImage(bmpImage))
{
// This draws the PNG onto the bitmap, scaling it if necessary.
// You may want to set the scaling quality
graphics->DrawImage(
imageSrc,
Rectangle(0,0, bmpImage.Width, bmpImage.Height),
Rectangle(0,0, imgSrc.Width, imgSrc.Height),
GraphicsUnitPixel
);
}
// You can now get the HBITMAP from the Bitmap object and use it.
// Don't forget to delete the graphics, image and bitmap when done.
关于c++ - 显示带有来自资源的 PNG 图标的 Win32 弹出菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9906561/