Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

MFC Tree State Manager using XML

, 21 May 2002
Save and Restore multiple tree states in your MFC applications
tbtreestatemgr_demo.zip
TreeStateSaverDemo
TreeStateSaverDemo.clw
res
TreeStateSaverDemo.ico
Toolbar.bmp
Debug
msxml.tlh
TreeStateSaverDemo.plg
TreeStateSaverDemo.dsp
TreeStateSaverDemo.aps
Release
msxml.tlh
CtlStateStg.ctss
TreeStateSaverDemo.res
vc60.idb
StdAfx.obj
TreeStateSaverDemo.obj
TBTreeStateMgr.obj
MainFrm.obj
ChildView.obj
TreeStateSaverDemo.exe
CtlStateStg.ctss
tbtreestatemgr_src.zip
// TreeStateMgr.h: interface for the TreeStateMgr class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_TREESTATEMGR_H__D810C7D2_D603_11D5_AC1A_00A00C90889A__INCLUDED_)
#define AFX_TREESTATEMGR_H__D810C7D2_D603_11D5_AC1A_00A00C90889A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#import "msxml.dll" named_guids raw_interfaces_only


#include <vector>
using namespace std;

/*
 ***********************************************************************
 *	TBTreeStateMgr:		Saves the state of tree controls
 *
 *  All methods are static
 *
 *  NOTE________________________________________________________________
 *	| I kindly request folks to add their revisions (name,date,comments)|
 *	|___________________________________________________________________|
 *
 *	Revisions
 *	---------
 *	5/21	Vivek_Rajan		Article for Codeproject
 ***********************************************************************
 */
class TBTreeStateMgr  
{
protected:
// [useful typedefs]
	typedef vector<BSTR>	BSTRVECT_T;

// [temp variables]
protected:
	static TCHAR			* m_StgFileName;		// IStorage name
	static CTreeCtrl		* pCtrl;				// Temp ptr to tree control
	static IStream			* pCurrStream;			// Current Stream 
	static IStorage			* pCurrStorage;			// Current storage
	static IXMLDOMDocument  * pDoc;					// XML Document
	static IXMLDOMNode		* pExpNodes;			// Expanded nodes
	static BSTRVECT_T		* v_bsStates;			// States vector 

	static CRITICAL_SECTION	  csection;				// Critical section
	static bool				  fInitialized;			// Need to init this class
	static bool				  fUsingCustomStorage;	// For memory tracking
// [client methods]
public:
	static void Initialize();
	static void Uninitialize();
	static void SetStorageFile(LPCTSTR lpszStorageName);
	static BOOL SaveTreeState (LPCTSTR lpszContext, CTreeCtrl * ctrl);
	static BOOL LoadTreeState (LPCTSTR lpszContext, CTreeCtrl * ctrl);

// [utility functions for this class]
protected:
	static BOOL			_SaveTreeState (LPCTSTR lpszContext, CTreeCtrl * ctrl);
	static BOOL			_LoadTreeState (LPCTSTR lpszContext, CTreeCtrl * ctrl);
	static void			DeepSaveItemState (HTREEITEM hItem);
	static HRESULT		OpenStorageFile(LPCTSTR lpszFn, bool fCreate=true);
	static IStream *	OpenStateStream(LPCTSTR lpszName, bool fCreate=true);
	static bool			IsExpandedState( LPCTSTR nodename);
	static HRESULT		LoadStateVector();
	static void			DeepSetItemState (HTREEITEM hItem);
	static char *		MakeValidName( LPCTSTR lpszContext);
	static IStream *	RecreateStream(LPCTSTR lpszName);

// [xml utility functions] private
private:
	static HRESULT		 CreateNewDocument(IXMLDOMDocument ** pDoc, IXMLDOMNode **pRoot);
	static IXMLDOMNode * AppendElementChild(IXMLDOMDocument * pDoc,IXMLDOMNode * pParent,LPCTSTR sNodeName);
	static IXMLDOMNode * AddNamedElementChild(IXMLDOMDocument * pDoc,IXMLDOMNode * pParent,LPCTSTR sNodeName,LPCSTR sNodeValue);
	static HRESULT		 CreateNewDocumentFromStream( IXMLDOMDocument **pDoc,IXMLDOMNode ** pRoot,IStream * pStm);
	static IXMLDOMNode * GetChildElementByName( IXMLDOMNode * pParent, LPCTSTR	childName);
};

#endif // !defined(AFX_TREESTATEMGR_H__D810C7D2_D603_11D5_AC1A_00A00C90889A__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Vivek Rajan
Web Developer
India India
C++/MFC/Java programmer, I like to work on high performance user interface programming.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 22 May 2002
Article Copyright 2002 by Vivek Rajan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid