tags:

views:

2196

answers:

4

I can use an bitmap in a menu

CMenu men;
CBitmap b;
b.LoadBitmap(IDB_0); 
men.AppendMenu( MF_ENABLED,1,&b);

I can draw an icon into a DC

  CImageList IL;
  IL.Create(70, 14, ILC_COLOR16 | ILC_MASK, 1, 0);
  IL.Add(AfxGetApp()->LoadIcon(IDI_0));
  IL.Draw ( pDC,  0,  rcIcon.TopLeft(),  ILD_BLEND50 );

But I cannot find a simple way to show an icon in a menu. I would like to do this

CMenu men;
CBitmap b;
// here the miracle happens, load the icon into the bitmap
men.AppendMenu( MF_ENABLED,1,&b);

Seems like this should be possible.


This is the same question as this. However that question referred to the MFC feature pack, did not get answered, and has shown no activity for a month, so I thought it would be worthwhile to ask it again in reference to basic MFC.

A: 

I asked the question you reference.

The way to add (normal, 16-bit color) icons to menus is to make a toolbar with the same resource id as the menu you want to have icons in. You then assign id's to each of the toolbar buttons, the same id's as the menu entries. Make a wizard-generated new MFC application and you'll see how it works there.

The answers to the question I posted suggested that it should work the same for 32-bit images with transparency for the Feature Pack toolbars; I haven't gotten around to test it out though.

If your specific problem is how to make dynamically-generated menus, I think you should pass the id of an existing entry in a toolbar and then that image will be used.

Not a real answer to your question but maybe it'll point you in the right direction.

Roel
Thank you. This sounds like it might work. However, it seems rather complicated, especially since I have not worked with toolbars before. For now, I will stick to my current workaround - storing a bitmap AND an icon for every image. Sigh!
ravenspoint
A: 

In order to set up a bitmap for a menu, you need to call CMenu::SetMenuItemInfo() for each item with something like this:

MENUITEMINFO mii;
mii.cbSize = sizeof mii;
mii.fMask = MIIM_BITMAP;
mii.hbmpItem = bitmapHandle;
menu.SetMenuItemInfo(menuItem,&mii,TRUE);

A further complication with doing this is that this is okay for 256 colour bitmaps, but not full colour 32bit RGBA bitmaps - these will work, but only on Vista, and then only if you present the bitmap as pre-computed RGBA.

In practice, in my code I get round this by using another feature of menu icons, which is to set the hbmpItem to HBMMENU_CALLBACK, which allows a callback to draw the bitmap: I do that for Windows XP and before. The code gets a bit too complicated to post here. As an example, you could have a look at my code at

http://www.ifarchive.org/if-archive/infocom/interpreters/frotz/WindowsFrotzSrc.zip

Look in "MenuBar.h" and "MenuBar.cpp", particularly the code around MenuBar::SetBitmaps().

DavidK
You seem to have misread the question. I know how to use a bitmap in a menu ( using a simpler method, posted in my question ). I was hoping to find out how to use an icon with an equally simple method.
ravenspoint
A: 

Good code. Please note that this shows the bitmap image but it is always good to remove the blank space left to the string (used for check/unchek) if you are showing the image. I did like this.

        MENUINFO mi;
     mi.cbSize = sizeof(mi);
     mi.fMask = MIM_STYLE;
     mi.dwStyle = MNS_NOCHECK;
     pcSubMenu->SetMenuInfo(&mi);

     MENUITEMINFO mii;
     mii.cbSize = sizeof mii;
     mii.fMask = MIIM_BITMAP;

     mii.hbmpItem = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_16_HELP),IMAGE_BITMAP,0,0,LR_SHARED |LR_VGACOLOR |LR_LOADTRANSPARENT);
     pcSubMenu->SetMenuItemInfo(ID_CONTENTS,&mii,FALSE);
A: 

I think what you are looking for is very similar to what's described here... www.codeguru.com/cpp/controls/menu/bitmappedmenus/article.php/c165/

mixed with what's described in www.codeproject.com/KB/shell/DynIcon.aspx

Still have to see if it will work.

Tamer