Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing image of a menu button in a CMFCToolbar

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.

like image 769
Ryan Avatar asked Mar 10 '09 16:03

Ryan


1 Answers

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

like image 104
Kirill V. Lyadvinsky Avatar answered Oct 30 '22 12:10

Kirill V. Lyadvinsky