1

First off, I'm not a C++ developer, I program mainly in C#. I am trying to use the ContextMenu (yes I'm aware of ContextMenuStrip) class in .NET which is a wrapper around CMenu in my application.

How do I display an image next to a menu item that has proper transparency?

What I've done so far is call SetMenuItemBitmaps on my menu item, here is the attached result:

enter image description here

As you can see there is a white background on the menu item. This is just so frustratingly close to what I want. How can I get the transparency issues to go away? I've attached the exact image I used below on this menu item:

enter image description here

I know that SetMenuItemBitmaps is not meant for colored images.

The selected and clear bitmaps should be monochrome. The system uses the Boolean AND operator to combine bitmaps with the menu so that the white part becomes transparent and the black part becomes the menu-item color. If you use color bitmaps, the results may be undesirable.

So what is the proper (hopefully simple) way of doing this?

EDIT

Another thing I tried was calling SetMenuItemInfo, but I keep getting error 87 (invalid parameter).

This is what the MENUITEMINFO structure looks like before I call SetMenuItemInfo:

enter image description here

And the values for the parameters I call on SetMenuItemInfo are:

hMenu - The handle to the ContextMenu?

uItem - 0 (the first item index?)

fByPosition - true

lpmii - (the structure contents above)

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
test
  • 2,589
  • 2
  • 24
  • 52
  • There's nothing obviously wrong with the structure that I can see, so maybe check that `hMenu` is valid? Maybe test by calling `GetMenuItemInfo()` to get something simple (`fState` for example). – Jonathan Potter Oct 10 '13 at 22:49
  • @JonathanPotter Well I was able to get SetMenuItemInfo to work, thanks for the direction. The only problem is that it looks the exact same as the above (pixelated with white background). – test Oct 10 '13 at 23:02
  • Is your bitmap 32bpp with pre-multiplied alpha? – Jonathan Potter Oct 10 '13 at 23:09
  • @JonathanPotter I followed this tutorial for creating a pre-multiplied alpha picture in GIMP: http://kevin.c.krinke.ca/2012/04/24/creating-images-with-pre-multiplied-alpha-using-gimp/ using the png I attached in my post, so yes, I believe so. – test Oct 10 '13 at 23:13
  • How are you getting that PNG into an HBITMAP? – Jonathan Potter Oct 10 '13 at 23:15
  • @JonathanPotter In managed code I call GetHbitmap: http://msdn.microsoft.com/en-us/library/1dz311e4.aspx – test Oct 10 '13 at 23:18
  • It seems like `GetHbitmap` loses the alpha channel - see e.g. http://stackoverflow.com/questions/9275738/convert-hbitmap-to-bitmap-preserving-alpha-channel – Jonathan Potter Oct 10 '13 at 23:29
  • @JonathanPotter Hey Jonathan, you've been a great help. I posted a question [here](http://stackoverflow.com/questions/19322926/loading-image-onto-menuitem-is-losing-transparency-on-pre-multiplied-alpha-image) outlining the problem I'm currently facing. If you get a chance to look, I'd be very grateful. – test Oct 11 '13 at 16:32

1 Answers1

1

When you add the menu item, simply set the MIIM_BITMAP flag and pass the HBITMAP in the hbmpItem member of the MENUITEMINFO structure. If your bitmap is 32bpp with pre-multiplied alpha then it will be displayed correctly.

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • Sorry to do this to you, but can you provide me an example of a proper call to SetMenuItemInfo which does what you've said? I actually attempted what you've described as my first attempt but could not get it to work. – test Oct 10 '13 at 22:27
  • `mi.fMask |= MIIM_BITMAP; mi.hbmpItem = hbmp;` – Jonathan Potter Oct 10 '13 at 22:28
  • Yeah I figured that part, I just am not actually creating the menu in C++. It is a C# application so I have to pinvoke to do this. I'm not 100% sure how I would call SetMenuItemInfo and also populate the MENUITEMINFO structure. I consistently get error 87 (invalid parameter). – test Oct 10 '13 at 22:32
  • Maybe post some code (as another question, probably). And you should tag as C# if you're using C#. – Jonathan Potter Oct 10 '13 at 22:33
  • I've made an edit, if you could take a look I'd greatly appreciate it. – test Oct 10 '13 at 22:43