Show popup Win32 menu with PNG icons from resources

It's been a long time since I had to deal with Win32 menus. I need to add some PNG icons to the context menu of a Win32 context. Naturally I want to preserve the transparency of the PNG and all the pixel alpha in the process. Is it possible?

I thought about using SetMenuItemBitmaps

. Is this the way?

I imported my PNGs as "PNG" assets, but I cannot load them with LoadBitmap

or with LoadImage

. I found a few suggestions for use Gdi+

, but obviously I won't draw the menu - the system will.

There seems to be a way to get HBITMAP

from Gdi + Bitmap

, but it looks like all alpha is lost in the process. AFAIK, a HBITMAP

can happily post alpha information.


source to share

2 answers

You need GDI + to load PNG. Then you need to create a 32-bit alpha bitmap of the correct size, create the graphics in the bitmap, and use DrawImage to copy the PNG to the bitmap. This gives you a bitmap with an alpha channel.

Something like that:

Image*  pimgSrc = Image::FromFile("MyIcon.png"L, FALSE);
Bitmap* pbmpImage = new Bitmap(
    iWidth, iHeight, 
Graphics* pgraphics = Graphics::FromImage(bmpImage))
    // This draws the PNG onto the bitmap, scaling it if necessary.
    // You may want to set the scaling quality 
        Rectangle(0,0, bmpImage.Width, bmpImage.Height),
        Rectangle(0,0, imgSrc.Width, imgSrc.Height),
// 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.




Perhaps you could use an icon instead?

Here are my reasons for using icons instead of PNGs:

  • The Win32 API has good support icons and it is relatively easy to draw icons since no GDI + is required.
  • Icons also support 8-bit transparency (like PNG).
  • Icons can be any pixel size (like PNG).
  • Icons can be easily embedded into an executable file as a resource.
  • Icons can be edited using Visual Studio.

To load an icon from a resource or file, use:



To draw an icon use:

DrawIcon() or DrawIconEx()




All Articles