10

I have a menu button inside a CMFCToolbar and I would like to replace the bitmap of the button each time a different entry is selected in the menu as each entry has its own icon.

I succeed in changing the icon using CMFCToolBarMenuButton::SetImage but it changes the icon in the menu entry too. Too bad.

alt text http://www.freeimagehosting.net/uploads/137269b0f2.jpg alt text http://www.freeimagehosting.net/uploads/879d03843a.jpg

Here is a sample of code:

if ( (pToolbar != NULL) && (idBase != 0) )
{
    int ixButtonToReplace                   = pToolbar->CommandToIndex(idBase);
    CMFCToolBarMenuButton* pBtnToReplace    = dynamic_cast<CMFCToolBarMenuButton*>
                                                (pToolbar->GetButton(ixButtonToReplace));
    if ( pBtnToReplace )
    {
        const CObList& listCommands = pBtnToReplace->GetCommands();
        POSITION pos                = listCommands.GetHeadPosition();
        while ( pos != NULL )
        {
            CMFCToolBarMenuButton* pItem = (CMFCToolBarMenuButton*) listCommands.GetNext(pos);
            if ( pItem && (pItem->m_nID == idButtonToReplaceWith) )
            {
                pBtnToReplace->SetImage(pItem->GetImage());
            }
        }
    }
}

Any ideas? Thank you.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Ryan
  • 477
  • 5
  • 12
  • 1
    Is the sequence: 12/Content is selected; click on dropdown; click on (X)/Name; Content's icon now changes to Name's? – enriquein Oct 01 '10 at 17:11

2 Answers2

2

It works out-of-box. The only you need is to call CMFCToolBar::AddToolBarForImageCollection so MFC could know which images to use.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
1

Not sure what you mean by the menu button is changed too?

If another button is changed with the single setImage call to the obvious indication is they share a resource ID of some sort, the only solution would be to ensure they have different ID's (which would require making sure both resources are handled separately). But it's a long time since I messed in MFC resource files to confirm this.

NotJarvis
  • 1,267
  • 11
  • 17