- UID
- 3272
- 积分
- 0
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-3-23
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2002-5-18 10:18:48
|
显示全部楼层
求教!!
我想做的那个“材料成型CAD图纸管理”生成图纸数后,我想将整个数保存,用MFC我已搞定,但ARX不能啊,主要实现的是,在树视中打右建菜单使树项目保存。
关健代码如下:
在文档中:
void CTreeStoreDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
SerializeTreeView(ar);
}
void CTreeStoreDoc::SerializeTreeView(CArchive& ar)
{
//SAMPLE: Well, we don't know exactly which view needs
// to be serialized. For this demo, we'll find the first
// tree control there is and serialize it. This has the
// net effect of making the "New" command in the "Window"
// menu behave in a pretty broken way. In order to fix
// it, you'd want to override the creation of a new
// view in that response and have the new view copy data
// from the copied view. (This is a problem with all
// control-based views.)
POSITION pos = GetFirstViewPosition();
CManProjectVw1* pView = NULL;
while (pos != NULL && pView == NULL)
{
CView* pTemp = GetNextView(pos);
pView = DYNAMIC_DOWNCAST(CManProjectVw1, pTemp);
}
if (pView == NULL)
{
MessageBox(NULL, _T("Couldn't find a view to serialize!"),
AfxGetAppName(), MB_OK | MB_ICONHAND);
}
else
{
pView->Serialize(ar);
}
}
void CManProjectVw1::OnItemSave()
{
CTreeStoreDoc *pDoc=GetDocument();
CFileDialog dlg(TRUE); // FALSE = save dialog
CString szFilter;
LPCTSTR lpszPathName;
szFilter = "材料成型CAD项目文件 (.clcx)|*.clcx|";
szFilter += "All Files (*.*)|*.*|";
LPTSTR pch = szFilter.GetBuffer(0);
while ((pch = _tcschr(pch, '|')) != NULL)
*pch++ = '\0';
dlg.m_ofn.lpstrFilter = szFilter;
dlg.m_ofn.nFilterIndex = 11;
dlg.m_ofn.lpstrTitle = "保存材料成型CAD项目文件";
dlg.DoModal();
}
void CManProjectVw1::RecursiveWriteItems(CArchive& ar, CTreeCtrl& refCtrl, HTREEITEM hItem)
{
//SAMPLE: loop through each item at the level of hItem (eg,
// hItem and all of its siblings)
do
{
CString str = refCtrl.GetItemText(hItem);
ar << (BYTE) recordRegular;
ar << str;
//SAMPLE: if a given item has children, mark a push in the indentation
// level and recurse to get all those children
if (refCtrl.ItemHasChildren(hItem))
{
ar << (BYTE) recordPush;
RecursiveWriteItems(ar, refCtrl, refCtrl.GetChildItem(hItem));
}
}
while ((hItem = refCtrl.GetNextSiblingItem(hItem)) != NULL);
//SAMPLE: done working at this level--mark a pop and return
// either to our caller or the next less nested level.
ar << (BYTE) recordPop;
}
void CManProjectVw1::WriteTreeViewContent(CArchive& ar, CTreeCtrl& refCtrl)
{
UINT nCount = refCtrl.GetCount();
ar << nCount;
// short circuit the zero-element case
if (nCount == 0)
return;
//SAMPLE: there's real work to do! Start recursing at the root item
HTREEITEM hItem = refCtrl.GetRootItem();
RecursiveWriteItems(ar, refCtrl, hItem);
}
void CManProjectVw1::ReadTreeViewContent(CArchive& ar, CTreeCtrl& refCtrl)
{
//SAMPLE: start out by deleting all the content from the control
refCtrl.DeleteAllItems();
//SAMPLE: figure out if the control was empty when it was stored
UINT nCount;
ar >> nCount;
//SAMPLE: if so, just short circuit the zero-element case
if (nCount == 0)
return;
//SAMPLE: otherwise, start sucking data in!
m_nIndex = 0;
m_hItems[m_nIndex] = TVI_ROOT;
HTREEITEM hAfter = TVI_FIRST;
//SAMPLE: while we're above the root, keep reading
while (m_nIndex >= 0)
{
//SAMPLE: get a record type
BYTE byteType;
ar >> byteType;
switch (byteType)
{
//SAMPLE: for a regular record, we'll just insert it into
// the control. Note how we'll insert after the most recently
// added item, and we'll insert as a child of the item on
// our hItems stack.
case recordRegular:
{
CString str;
ar >> str;
hAfter = refCtrl.InsertItem(str, m_hItems[m_nIndex], hAfter);
}
break;
//SAMPLE: if we're going one deeper, we'll need to bump the stack
// and reset the insertion flag. Note that this sample can only
// handle 99 levels of items in the control. You might want to fix
// this code to use a dynamic array (eg, a CArray). Whatever you do,
// the error recovery here needs to be a bit better!
case recordPush:
ASSERT(m_nIndex < 99);
m_hItems[++m_nIndex] = hAfter;
hAfter = TVI_FIRST;
break;
//SAMPLE: if we're going one less deep, we'll pop the stack
case recordPop:
m_nIndex--;
break;
default:
{
ASSERT(FALSE);
//TODO: pick a better exception type
AfxThrowMemoryException();
}
}
}
}
void CManProjectVw1::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
WriteTreeViewContent(ar, GetTreeCtrl());
}
else
{
ReadTreeViewContent(ar, GetTreeCtrl());
}
} |
|