Microsoft KB Archive/141675

= HOWTO: How to Save and Restore State of Dockable Toolbars =

Article ID: 141675

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 2.0 Professional Edition

 Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2

 Microsoft Visual C++ 4.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q141675

<div class="summary_section">

SUMMARY
MFC's CToolBar class makes it easy to create floating toolbars that can dock to your window. Each time you run your application, these toolbars will be re-created in the default locations. This article shows how to override this behavior to save the toolbar positions and docking state and restore them the next time the application is executed.

<div class="moreinformation_section">

MORE INFORMATION
When you create a SDI or MDI MFC application with the AppWizard, you can choose the Docking Toolbar option (default is selected). This option creates a default toolbar that can float and has tooltips. It adds a CToolBar member m_wndToolBar to the CMainFrame Class. It also adds code to CMainFrame::OnCreate to create the toolbar and set its attributes.

CFrameWnd has member functions that can save and restore the state of all control bars. The functions are SaveBarState and LoadBarState. There are two other helper functions, GetDockState and SetDockState.

To save the state of your control bars, you need to add a handler for CMainFrame::OnClose: void CMainFrame::OnClose {               SaveBarState( "MyDockState" );

CFrameWnd::OnClose; } This saves the docking state in the registry or .ini file under the MyDockState profile entry.

LoadBarState must be called to reload the information and set the position of the controlbars next time the program is executed. This call has to be added to CMainFrame::OnCreate. The following code illustrates this in the context of OnCreate: int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {

...

//Add this line... LoadBarState( "MyDockState" ); }

<div class="references_section">