![]() |
Desktop Development »
Tree Controls »
General
Intermediate
MFC Tree State Manager using XMLBy Vivek RajanSave and Restore multiple tree states in your MFC applications |
VC6, VC7Win2K, WinXP, Visual Studio, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
A vast majority of successful applications use the tree control to display a variety of information to the user. The tree control has become so ubiquitous that the users are very familiar with its operations. One of the common problems with the tree control is saving the state between sessions. This article presents a class
TBTreeStateMgr that makes it effortless to save and restore the state of multiple trees.
The tree state consists of
TBTreeStateMgr only stores the expanded state.
The following are the highlights of TBTreeStateMgr implementation:
TBTreeStateMgr are declared static. This is due to the fact that a single instance of
TBTreeStateMgr can handle storage of a number of tree states.
TBTreeStateMgr is threadsafe
#include "TBTreeStateMgr.h"
CoInitialize or OleInitialize is called in the
InitInstance function
/////////////////////// // Init COM CoInitialize(NULL);
InitInstance function of your MFC application
/////////////////////////////////// // Initialize the TBTreeStateMgr // TBTreeStateMgr::Initialize();
/////////////////////////////////// // Uninitialize TBTreeStateMgr::Uninitialize();
BOOL TBTreeStateMgr::SaveTreeState(LPCTSTR lpszTreeContextName,CTreeCtrl * pTreeCtrl);Note: This function is typically called before quitting the window. For example: by processing the WM_CLOSE message.
//This call saves the specified tree instance. "My Tree1 View" is the name of the instance. TBTreeStateMgr::SaveTreeState(_T("My Tree1 View"),&wnd_TreeCtrl);
BOOL TBTreeStateMgr::LoadTreeState(LPCTSTR lpszTreeContextName,CTreeCtrl * pTreeCtrl);
Note: This function is typically called just after initializing the tree control with the data For example: in the OnInitialUpdate() function
//This call restores the wnd_TreeCtrl to a state stored under the name <code>"My Tree1 View" TBTreeStateMgr::LoadTreeState(_T("My Tree1 View"),&wnd_TreeCtrl);
The TBTreeStateMgr by default stores the Structured Storage file in the current directory under the file name "CTLStateStg.ctss", if you want to specify a different directory or path use the
SetStorageFile(LPCTSTR lpszNewFile)
// Change the storage file location TBTreeStateMgr::SetStorageFile(_T("e:\\MyDir\\ILikeThisDir\\MyStorageFile.strg"));
IStorage/IStream in this project will demonstrate how powerful these tools are. Structured Storage is in fact used by MSWord. The DOC format is actually a
IStorage compatible file.
While I was evaluating techniques to store the tree control state, I stumbled upon XML. XML is also highly tree structured. If we could somehow transfer the tree state to an XML document efficiently and back, we could have a really cool solution. The MSXML parser is highly efficient and widely available with IE5 deployments. The bulk of the code in
TBTreeStateMgr is devoted to translating the tree state to XML and back. The XML DTD for this project is:
<!-- TBTreeStateMgr.dtd -- The DTD for storing tree state -->
<!ELEMENT TreeState(ExpandedNodes)>
<!ELEMENT ExpandedNodes(node*)>
<!ELEMENT node (#PCDATA)>
<!-- . . end . . -->
The next major problem was : How to store multiple trees ?
If you open the CtlState.ctss using the "Docfile Viewer" we can see the following structure:
As the above figure shows, the DOCfile stores each XML document in a separate stream. The reason for this architecture is: Any large scale application would have to deal with multiple trees (or) with multiple users view of the same tree. An application may also have trees in the views of multiple documents. For example: Each book document can have a tree view showing the table of contents. If we created a different XML Document for each tree instance, we may end up with hundreds of files. Another reassuring factor was Structured Storage is used extensively by MS in its own products (MSWord/Excel store DOC,XLS files in Structured Storage)
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 21 May 2002 Editor: Chris Maunder |
Copyright 2002 by Vivek Rajan Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |