发信人: jiangsheng()
整理人: wenbobo(2002-12-06 22:33:17), 站内信件
|
【 在 wangf (原) 的大作中提到: 】
: 请问有没有比较方面的方法把一个深层次的树保存在一个文件中,再次读入时
: 有能很快的按照上次的层次排列显示出来?
Project Name:E ,SDI interface
class CEDoc :public CDocument;
class CEView:public CTreeview;
void CEDoc::Serialize(CArchive& ar)
{
POSITION position=GetFirstViewPosition ();
//get treeview
CEView* view=(CEView* )this->GetNextView (position);
CString strItemText;
HTREEITEM hCurrent=NULL;
HTREEITEM hTest=NULL;
//ctreectrl* m_ptree
m_ptree=&(view->GetTreeCtrl ());
if (ar.IsStoring())
{
// TODO: add storing code here
//ar<<m_ptree->GetCount();
//UINT m_iNest
//to save position of next item relative to this ite m
m_iNest=0;
if(m_ptree->GetCount()=0)//no items
return;
//access the whole tree
hCurrent=m_ptree->GetRootItem( );
m_iDirection=CHILD;
while(m_iDirection!=OVER)
{
switch(m_iDirection)
{
case CHILD:
char szBuffer[256];
TV_ITEM item;
item.hItem = hCurrent;
item.mask = TVIF_TEXT | TVIF_CHILDREN;
item.pszText = szBuffer;
item.cchTextMax = 255;
m_ptree->GetItem(&item);
//save information
ar<<m_iNest;
strItemText=szBuffer;
ar<<strItemText;
m_iDirection=CHILD;//get child item if possible
//getchild item
if( m_ptree->ItemHasChildren(hCurrent))
{
m_iNest=1;//next item is the child of this item
hCurrent= m_ptree->GetChildItem(hCurrent);
m_iDirection=CHILD;
break;
}
else//no child item
{
//set base item nest if item has no child
m_iNest=0;
// Try next sibling item.
hTest = m_ptree->GetNextSiblingItem(hCurrent);
if(hTest!=NULL)
{
hCurrent=hTest;
m_iDirection=CHILD;
break;
}
else
{
//No slibing, goto parent
hTest= m_ptree->GetParentItem(hCurrent);
if(hTest==NULL)//no parent,no sibling,i.e. end of tree
m_iDirection=OVER;
else
{
hCurrent=hTest;
m_iNest=-1;
m_iDirection=PARENT;
}
break;
}
}
case PARENT:
// Try next sibling item.
hTest = m_ptree->GetNextSiblingItem(hCurrent);
if(hTest!=NULL)
{
hCurrent=hTest;
m_iDirection=CHILD;
break;
}
else
{
//No slibing, goto parent
hTest= m_ptree->GetParentItem(hCurrent);
if(hTest==NULL)//no parent,no sibling,i.e.end of tree
{
m_iDirection=OVER;
break;
}
hCurrent=hTest;
m_iNest--;
m_iDirection=PARENT;
break;
}
}
}
ar<<32767;//end of file
}
else
{
// TODO: add loading code here
//clear contents
m_ptree->DeleteAllItems();
ar>>m_iNest;
//the first interger must be 0
if(m_iNest!=0){
MessageBox(NULL,"非法文件头!","错误",MB_OK);
return;
}
ar>>strItemText;
hCurrent=m_ptree->InsertItem(strItemText);
while(hCurrent!=NULL)
{
ar>>m_iNest;
if(m_iNest==32767)//end of file
return;
ar>>strItemText;
if(m_iNest==1)
{
hCurrent=m_ptree->InsertItem(strItemText,hCurrent);
m_ptree->EnsureVisible(hCurrent);
continue;
}
while(m_iNest<0)
{
hCurrent=m_ptree->GetParentItem(hCurrent);
m_iNest++;
}
//get parent;if root,set root.
hTest=m_ptree->GetParentItem(hCurrent);
if(hTest==NULL)
hTest=TVI_ROOT;
m_ptree->InsertItem(strItemText,hTest);
}
}
}
-- ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 203.93.7.52]
|
|