TimeRun Cit
TimeRun Cit

Reputation: 177

InsertMenu/AppendMenu - How to add Icons to menu and submenus using C++ and win32

I have written a shell extension dll context menu program using C++ and win32 programming. The development environment is Visual Studio 2008 and 2010. In the below sample code, I am trying to add menu icon for the main menu only. The menu icon is not showing for the main menu. (I need to add icons for all menu items.).

Please correct the below code.

QueryContextMenu(HMENU hmenu, UINT /*uInd*/, UINT idCmdFirst, UINT /*idCmdLast*/, UINT /*uFlags*/ )
{


    int id = 1;

    HBITMAP hBitmap = NULL;

    hBitmap = (HBITMAP)LoadImage((HMODULE)_AtlBaseModule.m_hInst,MAKEINTRESOURCE(IDB_MYBITMAP), IMAGE_BITMAP, 12, 12, 0);


    HMENU submenu = CreatePopupMenu();

    AppendMenu(submenu, MF_STRING|MF_ENABLED, uidCmdFirst + id++, L"XP");
    AppendMenu(submenu, MF_STRING|MF_ENABLED,uidCmdFirst + id++, L"VISTA");
    AppendMenu(submenu, MF_STRING|MF_ENABLED,uidCmdFirst + id++, L"Win 7");

    InsertMenu(hmenu, 4,MF_BYPOSITION|MF_POPUP, UINT(submenu), L"Windows");
    SetMenuItemBitmaps(hmenu,id++, MF_BITMAP, hBitmap,hBitmap);


    return id;
}

Upvotes: 4

Views: 10330

Answers (2)

ChrCury78
ChrCury78

Reputation: 437

Reading the definition of SetMenuItemBitmap https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setmenuitembitmaps there is not such a thing as a MF_BITMAP flag.

The options are MF_BYCOMMAND and MF_BYPOSITION. Seems like that you are trying to use MF_BYCOMMAND but have to use the same values already given to the MenuItens.

You have also to check for the format of the BITMAP. SetMenuItemBitmaps just acepts monochrome. I was googling to do the same and had success here. Thanks for showing the way.

Upvotes: 1

Gaurav Raj
Gaurav Raj

Reputation: 708

CreatePopUpMenu also works. You just had to use InsertMenu instead of AppendMenu.

Upvotes: 1

Related Questions