Simple way to show a bitmap next to a menu item in a CMenu with correct transparency

妖精的绣舞 提交于 2021-01-28 05:19:07

问题


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)


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!