Icons on menus of MFC Feature Pack classes
Asked Answered
H

5

8

There are three places where menus show up in the new MFC functionality (Feature Pack):

  • In menu bars (CMFCMenuBar)
  • In popup menus (CMFCPopupMenu)
  • In the 'dropdown menu' version of CMFCButton

I want to put icons (high-color and with transparancy) in the menus in all of them. I have found CFrameWndEx::OnDrawMenuImage() which I can use to custom draw the icons in front of the menu bar items. It's not very convenient, having to implement icon drawing in 2008, but it works. For the others I haven't found a solution yet. Is there an automagic way to set icons for menus?

Harwood answered 16/9, 2008 at 8:34 Comment(1)
So with the answer below you have been able to add icons to top level menu items (like the usual File, Edit, View, Help items)? Thanks!Dibb
T
4

This is how I got it to work:

First

, as the others said, create an invisible toolbar next to your main toolbar (I'm using the usual names based on AppWizard's names):

MainFrm.h:
class CMainFrame
{
    //...    
    CMFCToolBar m_wndToolBar;
    CMFCToolBar m_wndInvisibleToolBar;
    //...
};

MainFrm.cpp:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    //...

    // Normal, visible toolbar
    if(m_wndToolBar.Create(this,
        TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC))
    {
        VERIFY( m_wndToolBar.LoadToolBar(
            theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME) );

        // Only the docking makes the toolbar visible
        m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
        DockPane(&m_wndToolBar);
    }

    // Invisible toolbar; simply calling Create(this) seems to be enough
    if(m_wndInvisibleToolBar.Create(this))
    {
        // Just load, no docking and stuff
        VERIFY( m_wndInvisibleToolBar.LoadToolBar(IDR_OTHERTOOLBAR) );
    }
}

Second: The images and toolbar resources

IDR_MAINFRAME and IDR_MAINFRAME_256 were generated by AppWizard. The former is the ugly 16 color version and the latter is the interesting high color version.
Despite its name, if I remember correctly, even the AppWizard-generated image has 24bit color depth. The cool thing: Just replace it with a 32bit image and that'll work, too.

There is the invisible toolbar IDR_OTHERTOOLBAR: I created a toolbar with the resource editor. Just some dummy icons and the command IDs. VS then generated a bitmap which I replaced with my high color version. Done!

Note

Don't open the toolbars with the resource editor: It may have to convert it to 4bit before it can do anything with it. And even if you let it do that (because, behind Visual Studio's back, wou're going to replace the result with the high color image again, ha!), I found that it (sometimes?) simply cannot edit the toolbar. Very strange.
In that case I advise to directly edit the .rc file.

Toluene answered 31/7, 2009 at 16:36 Comment(2)
Thanks, this worked for me but I had to add the following line after m_wndInvisibleToolbar.LoadToolBar(): m_wndInvisibleToolbar.ShowPane(FALSE, FALSE, FALSE); - without that line, for some reason my application would hang when closing!Task
Additionally, be sure to call: m_wndInvisibleToolBar.SetMaskMode( true ); on the toolbar, otherwise it will show up as an uneditable toolbar in the your Tools/Customization command (e.g. your CMFCToolBarsCustomizeDialog object).Ourselves
D
2

I believe (but I may be wrong) that these classes are the same as the BCGToolbar classes that were included in MFC when Microsoft bought BCG. If so, you can create a toolbar with and use the same ID on a toolbar button as in the menu items you want to create icons for, and they should appear automatically. Of course, you don't have to actually display the toolbars.

Dado answered 16/9, 2008 at 14:6 Comment(0)
D
2

In BCGToolbar, it's enough to create a toolbar in the resources & load it (but not display the window), but the toolbar button must have the same ID as the menu item you want to link it to.

Dado answered 18/9, 2008 at 14:24 Comment(0)
A
2

Try using this function:

CMFCToolBar::AddToolBarForImageCollection(UINT uiResID,
   UINT uiBmpResID=0,
   UINT uiColdResID=0,
   UINT uiMenuResID=0,
   UINT uiDisabledResID=0,
   UINT uiMenuDisabledResID=0);

So e.g.:

CMFCToolBar::AddToolBarForImageCollection(IDR_TOOLBAROWNBITMAP_256);

Worked very well for me.

Alecalecia answered 6/8, 2009 at 16:13 Comment(0)
R
0

One thing that can catch a person by surprise is that for customizable (ie, non-locked) toolbars, the first toolbar you make, the framework splits up and turns into some sort of palette bitmap of all icons in the program. If you try to add more toolbars later (or different toolbars) that have bitmaps (or pngs) with a different color depth than that first one, they seem to fail because it can't add them to the same palette.

Roderich answered 23/4, 2009 at 23:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.