Menu

Unable to make menuitem bitmaps visible

Help
2015-02-12
2015-02-24
  • Robert Tausworthe

    I am having trouble putting a bitmap image on a menuitem directly. I notice that those menuitems that also have a toolbar item with a bitmap image (e.g., IDM_FILE_NEW) have that image automatically inserted in their MENUITEMs. But when I try to add a bitmap to a MENUITEM that does not, I fail to make one appear. The code below shows how I tried to do it, pretty much the same as in the article

    CMenu::SetMenuItemBitmaps
    https://msdn.microsoft.com/en-us/library/a17fx6as.aspx

    In resource.rc I correspond a bitmap ID with the file in which it appears

    IDM_COLOR_CHOICE BITMAP DISCARDABLE "res/crayon13.bmp"

    The file contains a 13 x 13 bitmap, as required by querying the GetMenuCheckMarkDimensions() function. The IDW_MAIN_MENU contains a view menu popup (I am modifying your FormDocView sample), to which I have added a MENUITEM to bring up a color choice dialog to set the background color.

    POPUP "&View"
    {
        ...
        MENUITEM "&Tool Bar",           IDW_VIEW_TOOLBAR, CHECKED
        MENUITEM "&Status Bar",           IDW_VIEW_STATUSBAR, CHECKED
        MENUITEM SEPARATOR
        MENUITEM "&Background Color",     IDM_COLOR_CHOICE
        ...
    }
    

    I've tried both blank and CHECKED versions. The bitmap does not overwrite the image area in either case. In the CHECKED case, the check mark appears.

    In the CMainFrame.h declaration file, I define

    CBitmap m_colorbmp;
    

    In the CMainFrame.cpp implementation file, in OnCreate(), as in the MSDN Library example, I enter

    CMenu   *mainmenu = GetFrameMenu(),
        *submenu = mainmenu->GetSubMenu(2);
    assert(m_colorbmp.LoadBitmap(IDM_COLOR_CHOICE));
    assert(submenu->SetMenuItemBitmaps(IDM_COLOR_CHOICE, MF_BYCOMMAND,
        &m_colorbmp, &m_colorbmp));
    

    On execution, no assert activates, so the file is loading and setting ok, I suppose. But when the mouse selects the view submenu on the menu bar, the Background Color MENUITEMshows a blank grey image only (not CHECKED) or a check mark (CHECKED).

    What am I doing wrong?

    Thank you for any help you have time to provide. It is much appreciated.

     
  • David

    David - 2015-02-14

    Hi Robert,

    For some unknown reason, your message was held up in pending. I'm not sure why. Anyway, moving on ...

    The menu items for frames use owner drawing by default. As a result, any changes attempted with SetMenuItemBitmaps will be ignored. The SetMenuItemBitmaps function is only intended to replace the image displayed for checkmarks in menu items which is why it is 13x13. I expect this is not what you intend.

    The two functions used by Win32++ to associate images with menu items are CFrame::AddMenuIcons and CFrame::AddMenuIcon. The AddMenuIcon function can be used to associate a 16x16 icon resource to the menu item. The AddMenuIcons function is used to associate a bitmap containing several images to menu items.

    AddMenuIcons is demonstrated in the Browser sample.
    AddMenuIcon is demonstrated in the DockTabbedMDI, MDIFrameDemo and TabDemo samples.

    Best regards,
    David

     
    • Robert Tausworthe

      That worked! Thank you very much. I spent several days trying everything but that. Interesting that the MSDN description is so misleading. Even when I made the bmp a 13x13 black/white image, as a check mark replacement, it ignored it. BTW: I also found that the menuitem must be unchecked for the AddMenuItem() function to work.

       
  • Daniel Pantea

    Daniel Pantea - 2015-02-24

    Hi David,
    thank your for this information, I was also looking for a way to add icons in a context menu.

    I have one question though as I looked on other examples how it's done and then in DockTabbedMDI I noticed:

    inline HICON CWinApp::LoadIcon(int nIDIcon) const
    // Loads the icon resource whose size conforms to the SM_CXICON and SM_CYICON system metric values
    {
        return ::LoadIcon(GetApp()->GetResourceHandle(), MAKEINTRESOURCE (nIDIcon));
    }
    

    Is this "GetApp()->" needed here? I think not, as we should use already the correct CWinApp object, right?
    Best regards,
    Daniel

     

Log in to post a comment.