locked
CMFCToolbar layout not restored properly between sessions RRS feed

  • Question

  • I'm having a problem with the CMFCToolbar class where the positions of the toolbars are not being restored properly between sessions.

    Here is a screen shot of how the toolbars are arranged before the app is closed:



    Here is a screen shot of how the toolbars are restored when the app is launched again:


    Notice the large gap that appears above the first toolbar and the second toolbar is now to the right of the main toolbar instead of beneath it. I was having problems with my app, so I generated this test app using the App Wizard, then I added the second toolbar manually. I would like to know if this is because there is something I am doing wrong, or if it is just due to the buggy nature of the feature pack code. Here is the code from my CMainFrame class which sets up the toolbars:

    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    { 
        if (CFrameWndEx::OnCreate(lpCreateStruct) == -1) 
            return -1; 
     
        BOOL bNameValid; 
     
        // set the visual manager used to draw all user interface elements 
        CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerOffice2007)); 
     
        // set the visual style to be used the by the visual manager 
        CMFCVisualManagerOffice2007::SetStyle(CMFCVisualManagerOffice2007::Office2007_LunaBlue); 
     
        if (!m_wndMenuBar.Create(this)) 
        { 
            TRACE0("Failed to create menubar\n"); 
            return -1;      // fail to create 
        } 
     
        m_wndMenuBar.SetPaneStyle(m_wndMenuBar.GetPaneStyle() | CBRS_SIZE_DYNAMIC | CBRS_TOOLTIPS | CBRS_FLYBY); 
     
        // prevent the menu bar from taking the focus on activation 
        CMFCPopupMenu::SetForceMenuFocus(FALSE); 
     
        if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || 
            !m_wndToolBar.LoadToolBar(theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME)) 
        { 
            TRACE0("Failed to create toolbar\n"); 
            return -1;      // fail to create 
        } 
     
        CString strToolBarName; 
        bNameValid = strToolBarName.LoadString(IDS_TOOLBAR_STANDARD); 
        ASSERT(bNameValid); 
        m_wndToolBar.SetWindowText(strToolBarName); 
     
        m_wndToolBar.EnableTextLabels(); 
     
        if (!m_wndToolBar2.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || 
            !m_wndToolBar2.LoadToolBar(IDR_TOOLBAR)) 
        { 
            TRACE0("Failed to create toolbar2\n"); 
            return -1;      // fail to create 
        } 
     
        CString strCustomize("Toolbar2"); 
        m_wndToolBar2.EnableCustomizeButton(TRUE, ID_VIEW_CUSTOMIZE, strCustomize); 
     
        // Allow user-defined toolbars operations: 
        InitUserToolbars(NULL, uiFirstUserToolBarId, uiLastUserToolBarId); 
     
        if (!m_wndStatusBar.Create(this)) 
        { 
            TRACE0("Failed to create status bar\n"); 
            return -1;      // fail to create 
        } 
        m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT)); 
     
        // TODO: Delete these five lines if you don't want the toolbar and menubar to be dockable 
        m_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY); 
        m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); 
        m_wndToolBar2.EnableDocking(CBRS_ALIGN_ANY); 
        EnableDocking(CBRS_ALIGN_ANY); 
        DockPane(&m_wndMenuBar); 
        DockPane(&m_wndToolBar); 
        DockPane(&m_wndToolBar2); 
     
     
        // enable Visual Studio 2005 style docking window behavior 
        CDockingManager::SetDockingMode(DT_SMART); 
        // enable Visual Studio 2005 style docking window auto-hide behavior 
        EnableAutoHidePanes(CBRS_ALIGN_ANY); 
     
        // Load menu item image (not placed on any standard toolbars): 
        CMFCToolBar::AddToolBarForImageCollection(IDR_MENU_IMAGES, theApp.m_bHiColorIcons ? IDB_MENU_IMAGES_24 : 0); 
     
        // create docking windows 
        if (!CreateDockingWindows()) 
        { 
            TRACE0("Failed to create docking windows\n"); 
            return -1; 
        } 
     
        m_wndFileView.EnableDocking(CBRS_ALIGN_ANY); 
        m_wndClassView.EnableDocking(CBRS_ALIGN_ANY); 
        DockPane(&m_wndFileView); 
        CDockablePane* pTabbedBar = NULL; 
        m_wndClassView.AttachToTabWnd(&m_wndFileView, DM_SHOW, TRUE, &pTabbedBar); 
        m_wndOutput.EnableDocking(CBRS_ALIGN_ANY); 
        DockPane(&m_wndOutput); 
     
     
        // Enable toolbar and docking window menu replacement 
        EnablePaneMenu(TRUE, ID_VIEW_CUSTOMIZE, strCustomize, ID_VIEW_TOOLBAR); 
     
        // enable quick (Alt+drag) toolbar customization 
        CMFCToolBar::EnableQuickCustomization(); 
     
        if (CMFCToolBar::GetUserImages() == NULL) 
        { 
            // load user-defined toolbar images 
            if (m_UserImages.Load(_T(".\\UserImages.bmp"))) 
            { 
                m_UserImages.SetImageSize(CSize(16, 16), FALSE); 
                CMFCToolBar::SetUserImages(&m_UserImages); 
            } 
        } 
     
        // enable menu personalization (most-recently used commands) 
        // TODO: define your own basic commands, ensuring that each pulldown menu has at least one basic command. 
        CList<UINT, UINT> lstBasicCommands; 
     
        lstBasicCommands.AddTail(ID_FILE_NEW); 
        lstBasicCommands.AddTail(ID_FILE_OPEN); 
        lstBasicCommands.AddTail(ID_FILE_SAVE); 
        lstBasicCommands.AddTail(ID_FILE_PRINT); 
        lstBasicCommands.AddTail(ID_APP_EXIT); 
        lstBasicCommands.AddTail(ID_EDIT_CUT); 
        lstBasicCommands.AddTail(ID_EDIT_PASTE); 
        lstBasicCommands.AddTail(ID_EDIT_UNDO); 
        lstBasicCommands.AddTail(ID_APP_ABOUT); 
        lstBasicCommands.AddTail(ID_VIEW_STATUS_BAR); 
        lstBasicCommands.AddTail(ID_VIEW_TOOLBAR); 
        lstBasicCommands.AddTail(ID_SORTING_SORTALPHABETIC); 
        lstBasicCommands.AddTail(ID_SORTING_SORTBYTYPE); 
        lstBasicCommands.AddTail(ID_SORTING_SORTBYACCESS); 
        lstBasicCommands.AddTail(ID_SORTING_GROUPBYTYPE); 
     
        CMFCToolBar::SetBasicCommands(lstBasicCommands); 
     
        return 0; 
    } 
    


    Wednesday, January 6, 2010 5:40 PM

Answers

  • Hello

    After some days reseaches and PG's help, the cause is that we did use a unique ID for the second toolbar. The second toolbar should be created with this line:
        if (!m_wndToolBar2.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC, CRect(1,1,1,1), AFX_IDW_MENUBAR+1) ||

    instead of this line:

        if (!m_wndToolBar2.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||

    Regards,
    Rong-Chun Zhang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 13, 2010 10:29 AM

All replies

  • Do you see this behavior in a default MFC project generated by VS2008 SP1?

    Could you strip off your own code line by line and see which code causes the problem?
    Dave
    Thursday, January 7, 2010 10:51 AM
  • Hello Avalanchis

    Thanks for you post on MSND forum.

    I can repro the issue with the code your post, I am performing research on this issue and will get back to you as soon as possible. I appreciate your patience.

    Regards,
    Rong-Chun Zhang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, January 7, 2010 11:25 AM
  • Hi - I think my information can help you:

    I found that you can position the Toolbars using the CMFCToolbar::AdjustLayout.
    Check here-> http://msdn.microsoft.com/en-us/library/bb984532.aspx

    CBasePane::AdjustLayout -> http://msdn.microsoft.com/en-us/library/bb982076.aspx

    Check also DOCKTOOL: http://msdn.microsoft.com/en-us/library/texyde9k(VS.80).aspx

    I hope this helps...

    Have a nice day...

    Best regards,
    Fisnik
    Coder24.com
    Thursday, January 7, 2010 11:53 AM
  • According to the first Image, the code shown in your post shall work pretty well, or am I wrong?
    Explain!


    Coder24.com
    Friday, January 8, 2010 1:45 PM
  • Hello

    After some days reseaches and PG's help, the cause is that we did use a unique ID for the second toolbar. The second toolbar should be created with this line:
        if (!m_wndToolBar2.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC, CRect(1,1,1,1), AFX_IDW_MENUBAR+1) ||

    instead of this line:

        if (!m_wndToolBar2.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||

    Regards,
    Rong-Chun Zhang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com 

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, January 13, 2010 10:29 AM
  • Hello Avalanchis

    Have you got any progress on this issue with the suggestion? If there is anything we can help, welcome to post here. 

    Regards,
    Rong-Chun Zhang
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, January 15, 2010 9:04 AM