adding icon to a context menu

Václav Jedlička
Václav Jedlička used Ask the Experts™
on
I am trying to add icon to a context menu in a MFC app.

this code adds a textual menu item, but how do I add the icon to it?
CMenu l_menu;
l_menu.CreatePopupMenu();
l_menu.AppendMenu(MF_STRING, ID_REPORT_COLUMN_NEW, _T("some string"));

Open in new window

Thank you
Vaclav
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi Václav Jedlička,

you can use SetMenuItemInfo for this - to do this you need to load a bitmap, then you can set it like this:
	HBITMAP hBmp = ...; // load the bitmap, i.e. from resources
	MENUITEMINFO info = { sizeof( MENUITEMINFO ) };

	info.fMask = MIIM_BITMAP;
	info.fType = MFT_BITMAP;
	info.hbmpItem = (HBITMAP)bmp->GetSafeHandle();

	l_menu.SetMenuItemInfo( ID_REPORT_COLUMN_NEW, &info, FALSE );

Open in new window

One thing is imported: you have to ensure the loaded bitmap isn't deleted as long as the menu is exists.

I hope this helps,

best regards,

ZOPPO

Author

Commented:
Hi Zoppo,
Thank you for your response.
What is the bmp on line 6?
Thanks
Vaclav
ah, sorry, it's a relict from my test ... the line should simply be this:
info.hbmpItem = hBmp;

Open in new window

And I forgot to mention that you even will have to delete the bitmap when the menu is deleted in order to avoid GDI-leaks.

Regards,

ZOPPO
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
for some reason I cannod load the bitmap, hBmp is null:
hBmp = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE("IDB_ADD"));

Open in new window

any idea what is wrong?
The way you use MAKEINTRESOURCE is wrong, this macro is used to convert an integer ID to a char-pointer which is used to find a resource ... so you need to pass your ID, not a string containing the ID (that string is not known in the resources, there only the number of the #define is stored) - so just remove the "-s within MAKEINTRESOURCE.

Author

Commented:
ok, I changed the code to this, but the icon does not appear:
	CMenu l_menu;
	l_menu.CreatePopupMenu();

	l_menu.AppendMenu(MF_STRING, ID_REPORT_COLUMN_NEW,	pTran->TranslateString(_T("ids_new_column")));

	hBmp = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_ADD)); 
	MENUITEMINFO info = { sizeof(MENUITEMINFO) };

	info.fMask = MIIM_BITMAP;
	info.fType = MFT_BITMAP;
	info.hbmpItem = hBmp;

	l_menu.SetMenuItemInfo(ID_REPORT_COLUMN_NEW, &info, FALSE);

Open in new window

Hm - this is strange, hard to say, what's going wrong, my working testing code looks quite similar ... I guess the bitmap is loaded now and hBmp != NULL, right? Maybe there's something unusual regarding the bitmap type - what kind of bitmap do you use (size or colordepth)?

I found there's one thing which is not really correct in my sample, but I think it's not the cause for it's not working: it's not needed to set the 'ftype' member, so line 10 in your code isn't needed. With my test both works, so I guess this doesn't fix the problem.

Sorry, I don't really know how to find the cause, from my understanding and my tests I would say this should work ...

Author

Commented:
I created a blank MFC app and your solution works there.
So I am going to mark your response as solution.

It seems in my app it did not work because I am using CodeJock library.
Anyway, could you please show how to delete the bitmap?

Thanks
Vaclav
You're welcome,

hm - sorry, I never heard about this library ...

Yes, it's easy, just call DeleteObject( hBmp ); after you don't need it anymore.

Best regards,

ZOPPO

Author

Commented:
Thank you!
Vaclav

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial