Changing image of a menu button in a CMFCToolbar
Asked Answered
B

2

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.

Baseline answered 10/3, 2009 at 16:11 Comment(1)
Is the sequence: 12/Content is selected; click on dropdown; click on (X)/Name; Content's icon now changes to Name's?Impulsion
P
2

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

Possie answered 30/5, 2011 at 11:50 Comment(0)
D
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.

Drabble answered 31/3, 2011 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.