|
-
October 3rd, 2010, 02:25 PM
#1
How to use CDocManager in CDocument OnSaveAs override ?
I have an SDI CRichEditView application with Doc/View architecture in which I have overridden the OnFileSave method in the CDocument class. I would like to use the CDocManager class to save the current document. After many hours of internet browsing and Visual Studio 2008 experimentation, I am turning to you in desparation.
I might add (without intending to mislead), that using the OnFileOpen method override presents no problem:
Code:
void CMyDoc::OnFileOpen()
{
char fname[MAX_PATH];
int length;
length = GetModuleFileName(NULL, fname, 2048);
PathRemoveFileSpec(fname);
char szFilters[]= "All Files (*.*)|*.*||";
CFileDialog fileDialog (TRUE, "*.*", "*.*",
OFN_FILEMUSTEXIST| OFN_HIDEREADONLY | OFN_NOCHANGEDIR , szFilters, NULL);
fileDialog.m_ofn.lpstrInitialDir = fname;
if( fileDialog.DoModal ()==IDOK )
{
m_csPathname = fileDialog.GetPathName();
m_csFilename = fileDialog.GetFileName();
CDocManager * pDocMgr = (CDocManager *)AfxGetApp()->m_pDocManager;
pDocMgr->OpenDocumentFile(m_csPathname);
m_csTitle = m_csFilename;
SetTitle(m_csTitle);
}
}// OnFileOpen()
However, when I attempt to do something similar with OnFileSaveAs, I am unable to weave may way through the morass of documentation involving CDocManager and CArchive. The only progress that I have made is to discover that the CArchive::m_pDocument is NULL and I have not figured out how to set it to the current document. What follows below is one of many failed attempts:
Code:
void CMyDoc::OnFileSaveAs()
{
char fname[MAX_PATH];
int length;
length = GetModuleFileName(NULL, fname, 2048);
PathRemoveFileSpec(fname);
char szFilters[]= "All Files (*.*)|*.*||";
CFileDialog fileDialog (FALSE, "*.*", "*.*",
OFN_FILEMUSTEXIST| OFN_HIDEREADONLY | OFN_NOCHANGEDIR , szFilters, NULL);
fileDialog.m_ofn.lpstrInitialDir = fname;
//fileDialog.m_ofn.lpstrInitialDir = m_csServerDir.GetBuffer(0); m_csServerDir.ReleaseBuffer();
if( fileDialog.DoModal ()==IDOK )
{
m_csPathname = fileDialog.GetPathName();
m_csFilename = fileDialog.GetFileName();
CDocManager * pDocMgr = (CDocManager *)AfxGetApp()->m_pDocManager;
//pDocMgr->OpenDocumentFile(m_csPathname);
//pDocMgr->SaveAllModified();
//pDocMgr->Serialize(CArchive &ar);
//pDocMgr->GetNextDocTemplate(0);
//CArchive ar;
//CArchive ar(CFile *pFile, UINT nMode, int nBufSize = 4096, void *lpBuf = 0);
//CArchive ar(const CArchive &arSrc);
//BOOL ar.IsStoring();
//POSITION pos = ar.m_pDocument->GetFirstViewPosition();
//pDocMgr->GetNextDocTemplate(pos)
//pDocMgr->Serialize(ar);
// from: http://msdn.microsoft.com/en-us/library/kz2bk3c2(v=VS.80).aspx
// The framework sets m_pDocument to the document being serialized when a user issues
// a File Open or Save command. If you serialize an Object Linking and Embedding (OLE)
// container document for reasons other than File Open or Save, you must explicitly set
// m_pDocument. For example, you would do this when serializing a container document to
// the Clipboard
CFile myFile(m_csPathname, CFile::modeCreate | CFile::modeWrite);
CArchive ar(&myFile, CArchive::store);
//POSITION pos = ar.m_pDocument->GetFirstViewPosition(); // APPCRASH
//pDocMgr->GetNextDocTemplate(pos);
//ar.m_pDocument->GetNextView(pos); // WHAT POSITION ?
//ar.m_pDocument->SetModifiedFlag();
//ar.m_pDocument->OnSaveDocument(LPCTSTR lpszPathName);
//ar.m_pDocument->OnSaveDocument(m_csPathname);
CMainFrame * pMain = (CMainFrame*)AfxGetMainWnd();
CWinAESView * pView = (CWinAESView*)pMain->GetActiveView();
//ar.m_pDocument->AddView(pView);
//ar.m_pDocument->GetDocTemplate();
if(ar.m_pDocument == NULL) MessageBox(NULL, "It's NULL!", "Save As", MB_OK);
// Serialize the document to the archive.
if (ar.m_pDocument != NULL)
ar.m_pDocument->Serialize(ar);
m_csTitle = m_csFilename;
SetTitle(m_csTitle);
}
}// OnFileSaveAs()
Note that the above method saves an empty file to the directory and that the m_pDocument is invariably NULL.
I am certain that there is a way to accomplish my task. Can anyone please help?
mpliam
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|