Link to home
Start Free TrialLog in
Avatar of Václav Jedlička
Václav Jedlička

asked on

adding icon to a context menu

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
Avatar of Zoppo
Zoppo
Flag of Germany image

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
Avatar of Václav Jedlička
Václav Jedlička

ASKER

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
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.
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

ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Thank you!
Vaclav