对于多文档程序,新建一个文档之后,点击保存按钮,将跳出一个文件对话框,选择文件保存路径。我想在文档创建的时候指定一个固定的文件,而不要用户选择存档路径,应该修改文档的哪些属性啊?我设置了m_strPathName,发现保存文档的时候mfc监测该路径下文件的属性,就是说文件不存在它还是要跳出文件选择对话框。我又创建了一个该名称的空文件,这下mfc不跳出那个文件对话框了,但是发现文件保存后,序列化过程也调用了,但是文件仍为0长度,还有什么地方需要修改呢?跟踪了半天,脑子都浆糊了
调试欢乐多
{
static int i = 0;if(i == 0 )
OnSaveDocument(文件名);
else
CDocument::OnFileSave() ;
}
static bool isFirst = TRUE;if(isFirst == TRUE)
OnSaveDocument("....");
else
CDocument::OnFileSave();
}
BOOL COleDocument::OnSaveDocument(LPCTSTR lpszPathName)
// lpszPathName must be fully qualified
{
USES_CONVERSION; ASSERT(lpszPathName == NULL || AfxIsValidString(lpszPathName)); // use default implementation if 'docfile' not enabled
if (!m_bCompoundFile && m_lpRootStg == NULL)<<----m_bCompoundFile是1,即为复合文档,所以CDocument::OnSaveDocument过程得不到调用,因而一致保存文件不成功,莫非是bug?
{
ASSERT(lpszPathName != NULL);
return CDocument::OnSaveDocument(lpszPathName);
} LPSTORAGE lpOrigStg = NULL;
if (lpszPathName != NULL)
m_bSameAsLoad = AfxComparePath(m_strPathName, lpszPathName); BOOL bResult = FALSE;
TRY
{
// open new root storage if necessary
if (lpszPathName != NULL && !m_bSameAsLoad)
{
// temporarily detach current storage
lpOrigStg = m_lpRootStg;
m_lpRootStg = NULL; LPSTORAGE lpStorage;
SCODE sc = ::StgCreateDocfile(T2COLE(lpszPathName),
STGM_READWRITE|STGM_TRANSACTED|STGM_SHARE_DENY_WRITE|STGM_CREATE,
0, &lpStorage);
if (sc != S_OK)
AfxThrowOleException(sc); ASSERT(lpStorage != NULL);
m_lpRootStg = lpStorage;
}
ASSERT(m_lpRootStg != NULL); // use helper to save to root storage
SaveToStorage(); if (lpszPathName != NULL)
{
// commit each of the items
CommitItems(m_bRemember && !m_bSameAsLoad); // document as clean if remembering the storage
if (m_bRemember)
SetModifiedFlag(FALSE); // remember correct storage or release save copy as storage
if (!m_bSameAsLoad)
{
if (m_bRemember)
{
// Save As case -- m_stgRoot is new storage, forget old storage
lpOrigStg->Release();
}
else
{
// Save Copy As case -- m_stgRoot should hook up to m_stgOrig.
m_lpRootStg->Release();
m_lpRootStg = lpOrigStg;
}
}
} bResult = TRUE;
}
CATCH_ALL(e)
{
if (lpOrigStg != NULL)
{
// save as failed: abort new storage, and re-attach original
RELEASE(m_lpRootStg);
m_lpRootStg = lpOrigStg;
} if (lpszPathName == NULL)
{
THROW_LAST();
ASSERT(FALSE); // not reached
} TRY
{
ReportSaveLoadException(lpszPathName, e,
TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
}
END_TRY
DELETE_EXCEPTION(e);
}
END_CATCH_ALL // cleanup
m_bSameAsLoad = TRUE;
m_bRemember = TRUE; return bResult;
}