Http://www.codeguru.com/Cpp/controls/treeview/misc-advanced/article.php/c713/
To serialize the Tree View control override the serialize () function. The serialize () function is a virtual function defined in cobject.
In the code below we save the outline to a text file and can read it back from a text file. when saving the outline to the archive, tabs are used to indent the item text. again, when reading back, tabs are used to determine the level that the newly read item shoshould be placed.
1 void ctreectrlx: serialize (carchive & AR)
2 {
3 if (AR. isstoring ())
4 {
5 // storing code
6 htreeitem HTI = getrootitem ();
7 while (HTI)
8 {
9 int indent = getindentlevel (HTI );
10 while (indent --)
11 ar. writestring ("\ t ");
12 ar. writestring (getitemtext (HTI) + "\ r \ n ");
13 HTI = getnextitem (HTI );
14}
15
16}
17 else
18 {
19 // Loading Code
20 cstring Sline;
21 if (! Ar. readstring (Sline ))
22 return;
23
24 htreeitem HTI = NULL;
25 int indent, baseindent = 0;
26 while (Sline [baseindent] = '\ t ')
27 baseindent ++;
28 do
29 {
30 if (Sline. getlength () = 0)
31. continue;
32 For (indent = 0; Sline [indent] = '\ T'; indent ++)
33; // we don't need a body
34 Sline = Sline. Right (Sline. getlength ()-indent );
35 indent-= baseindent;
36
37 htreeitem parent;
38 int previndent = getindentlevel (HTI );
39 if (indent = previndent)
40 parent = getparentitem (HTI );
41 else if (indent> previndent)
42 parent = HTI;
43 else
44 {
45 int nlevelsup = previndent-indent;
46 parent = getparentitem (HTI );
47 While (nlevelsup --)
48 parent = getparentitem (parent );
49}
50 HTI = insertitem (Sline, parent? Parent: tvi_root, tvi_last );
51} while (AR. readstring (Sline ));
52
53}
54}
55
56
57
58 int ctreectrlx: getindentlevel (htreeitem hitem)
59 {
60 int iindent = 0;
61
62 while (hitem = getparentitem (hitem ))! = NULL)
63 iindent ++;
64 return iindent;
65}
66
67
68 // getnextitem-Get next item as if outline was completely expanded
69 // returns-the item immediately below the reference item
70 // hitem-the reference item
71 htreeitem ctreectrlx: getnextitem (htreeitem hitem)
72 {
73 htreeitem HTI;
74
75 if (itemhaschildren (hitem ))
76 return getchilditem (hitem); // return first child
77 else {
78 // return next sibling item
79 // go up the tree to find a parent's sibling if needed.
80 while (HTI = getnextsiblingitem (hitem) = NULL ){
81 If (hitem = getparentitem (hitem) = NULL)
82 return NULL;
83}
84}
85 return HTI;
86}
// My new improvements apply to images
1 void CFileView::Serialize(CArchive& ar)
2 {
3 if (ar.IsStoring())
4 { // storing code
5 HTREEITEM hti = m_wndFileView.GetRootItem();
6 while( hti )
7 {
8 int indent = GetIndentLevel( hti );
9 while( indent-- )
10 ar.WriteString( _T("\t") );
11 int nImage = 0;
12 int nSelectedImage = 0;
13 m_wndFileView.GetItemImage(hti,nImage,nSelectedImage);
14 CString str;
15 str.Format(_T("%d\t%d\t"),nImage,nSelectedImage);
16 ar.WriteString( str + m_wndFileView.GetItemText( hti ) + "\r\n");
17 hti = GetNextItem( hti );
18 }
19 }
20 else
21 { // loading code
22 CString sLine;
23 CString nImage;
24 CString nSelectImage;
25 if( !ar.ReadString( sLine ) )
26 return;
27
28 HTREEITEM hti = NULL;
29 int indent, baseindent = 0;
30 while( sLine[baseindent] == '\t' )
31 baseindent++;
32 do
33 {
34 if( sLine.GetLength() == 0 )
35 continue;
36 for( indent = 0; sLine[indent] == '\t'; indent++ )
37 ; // We don't need a body
38 sLine = sLine.Right( sLine.GetLength() - indent );
39 nImage = sLine[0];
40 nSelectImage = sLine[2];
41 sLine = sLine.Right(sLine.GetLength() - 4);
42 indent -= baseindent;
43
44 HTREEITEM parent;
45 int previndent = GetIndentLevel( hti );
46 if( indent == previndent)
47 parent = m_wndFileView.GetParentItem( hti );
48 else if( indent > previndent )
49 parent = hti;
50 else
51 {
52 int nLevelsUp = previndent - indent;
53 parent = m_wndFileView.GetParentItem( hti );
54 while( nLevelsUp-- )
55 parent = m_wndFileView.GetParentItem( parent );
56 }
57 hti = m_wndFileView.InsertItem( sLine,_ttoi(nImage),_ttoi(nSelectImage), parent ? parent : TVI_ROOT, TVI_LAST );
58 }while( ar.ReadString( sLine ) );
59 }