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?
Vaclav
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"));
Thank youVaclav
ASKER
Hi Zoppo,
Thank you for your response.
What is the bmp on line 6?
Thanks
Vaclav
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:
Regards,
ZOPPO
info.hbmpItem = hBmp;
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
ASKER
for some reason I cannod load the bitmap, hBmp is null:
hBmp = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE("IDB_ADD"));
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.
ASKER
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);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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
ASKER
Thank you!
Vaclav
Vaclav
you can use SetMenuItemInfo for this - to do this you need to load a bitmap, then you can set it like this:
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