How to SetOriginalState of a dynamically created CMFCToolbar?

483 Views Asked by At

I have create some toolbars dynamically in my mfc application

m_cToolBarEx.CreateEx(this, TBSTYLE_FLAT , WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC | CBRS_HIDE_INPLACE);

the toolbar has no bitmap or resource id. I used the function InsertButton to add buttons in my toolbar

When I try to reset this toolbar through Reset Toolbar button from menu. The toolbar does not reset to its original state only the message box is prompted and no changes are restored.

I assume the problem is when the CMFCToolBar::RestoreOriginalstate() is executed the condtion:

if (m_uiOriginalResID == 0)
    {
        return FALSE;
    }

gets true and the function returns false as there is no resource id in the m_uiOriginalResID.

Is there any way to load the dynamically created toolbar or I have to inherit RestoreOriginalstate function and write my own.

1

There are 1 best solutions below

5
On BEST ANSWER

You should override RestoreOriginalstate() as stated in the Note section:

This method is called when the user selects Reset from the customization menu.You can also manually call this method to programmatically reset the state of the menu bar.This method loads the original state from the resource file.

Override this method if you want to do any processing when the user selects the Reset option.

You should also override CanBeRestored() function, the defaut implementation returns FALSE if the resource ID is 0.

Here is an exemple of RestoreOriginalstate() :

BOOL CLinksBar::RestoreOriginalstate ()
{
    RemoveAllButtons ();

    InsertButton (CLinkButton (_T("MSDN Home"), _T("http://www.msdn.com")));
    InsertButton (CLinkButton (_T("Microsoft Home"), _T("http://www.microsoft.com")));
    InsertButton (CLinkButton (_T("Visual C++ Developer Center"), _T("http://msdn2.microsoft.com/visualc/")));

    EnableCustomizeButton (TRUE, -1, _T(""));

    AdjustLayout ();
    Invalidate ();

    return TRUE;
}