问题
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:
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:
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:
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)
回答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.
来源:https://stackoverflow.com/questions/19307205/simple-way-to-show-a-bitmap-next-to-a-menu-item-in-a-cmenu-with-correct-transpar