0

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.

Fatima A.
  • 41
  • 13
  • As the [documentation](https://learn.microsoft.com/en-us/cpp/mfc/reference/cmfctoolbar-class#restoreoriginalstate) points out: *"This method loads the toolbar **from the resource file** [...]"*. If your toolbar isn't defined in a resource, you cannot use this class member. After all, where should the system load the layout from? You are going to have to recreate your toolbar manually, if you there is no resource to load from. Try to get a copy of Petzold's *"Programming Windows"* to learn about resources. – IInspectable Sep 12 '18 at 12:35

1 Answers1

0

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;
} 
Community
  • 1
  • 1
tunglt
  • 1,022
  • 1
  • 9
  • 16
  • while overriding this function i am using 'MFCToolBarButton* pButton = (CMFCToolBarButton*) m_Buttons.GetHead();' and pButton->SetVisible(); to make the first button visible and it works. Can anyone tell how traverse through all buttons like this and set them visible? – Fatima A. Sep 12 '18 at 13:57
  • @fat: If you have a new question, click the [Ask Question](https://stackoverflow.com/questions/ask) button. – IInspectable Sep 12 '18 at 14:04
  • @IInspectable No, its not a new question. The solution to the problem is not yet found. – Fatima A. Sep 12 '18 at 17:18
  • @fat: You accepted this answer, thereby indicating, that it solved your issue (ignoring the fake code). The question you now asked may be part of a larger problem you are trying to solve, but it is a new question, so use the [Ask Question](https://stackoverflow.com/questions/ask) button. – IInspectable Sep 12 '18 at 17:27
  • @fat: to set visible all buttons in your collection m_Buttons, using a for loop is an advice, if it does not help, please show us your codes in an other question. – tunglt Sep 12 '18 at 20:45