c++ - 显示带有来自资源的 PNG 图标的 Win32 弹出菜单

标签 c++ winapi

我已经有很长时间没有处理 Win32 菜单了。我需要将一些 PNG 图标添加到 Win32 上下文弹出菜单中。自然地,我想在此过程中保留 PNG 透明度和所有每像素 alpha。这可能吗?

我正在考虑使用 SetMenuItemBitmaps。这是要走的路吗?

我将我的 PNG 作为“PNG”资源导入,但我似乎无法使用 LoadBitmapLoadImage 加载它们。我找到了一些关于使用 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/

相关文章:

c++ - 遍历 MultiMap 以找到从给定值到给定键的路径

android - 在不使用 qmake 的情况下在 Android 的 Qt 应用程序上使用自定义 AndroidManifest?

c++ - std::rethrow_exception 和抛出的异常类型

c++ - 编辑控件捕获回车键

c++ - 禁止 Boost 二进制文件(通过猎人下载)

c++ - 实现 Thrust 二元谓词

c++ - 如何使用 Win32 Hook 模拟 Alt+Tab?

c++ - 如何将文件从回收站还原到特定位置?

c++ - 奇怪的测试结果。当我不使用时,我的 win32 程序运行得更慢

c++ - 在 win32 窗口中使用 OpenGL 渲染图像