在我的上一个问题(MFC: how to change default file name for CFileDialog?)中,我重载了DoSave函数以提供文件名建议。在这个函数中,有没有办法区分“保存”和“另存为”?因为我应该只弹出对话框窗口,如果它是“另存为”。
BOOL CMyDoc::DoSave(LPCTSTR lpszPathName, BOOL bReplace)
{
if (IsSaveAS() || m_saved_file_name.IsEmpty())
{
CString file_name = some_suggested_file_name;
CFileDialog file_dialog(false, ..., file_name, ...);
if (file_dialog.DoModal() == IDOK)
{
m_saved_file_name = file_dialog.GetPathName();
}
}
OnSaveDocument(m_saved_file_name);
return TRUE;
}发布于 2020-06-02 03:25:11
当lpszPathName == NULL || *lpszPathName == '\0'时,该对话框应弹出。
这涵盖了从OnFileSaveAs调用DoSave的情况
void CDocument::OnFileSaveAs()
{
if(!DoSave(NULL))
TRACE(traceAppMsg, 0, "Warning: File save-as failed.\n");
}以及关联文件是只读的并且不能被写入的情况。
BOOL CDocument::DoFileSave()
{
DWORD dwAttrib = GetFileAttributes(m_strPathName);
if (dwAttrib & FILE_ATTRIBUTE_READONLY)
{
// we do not have read-write access or the file does not (now) exist
if (!DoSave(NULL))
{
TRACE(traceAppMsg, 0, "Warning: File save with new name failed.\n");
return FALSE;
}
}
else
{
if (!DoSave(m_strPathName))
{
TRACE(traceAppMsg, 0, "Warning: File save failed.\n");
return FALSE;
}
}
return TRUE;
}它还与内置DoSave的逻辑相匹配。
BOOL CDocument::DoSave(LPCTSTR lpszPathName, BOOL bReplace)
// Save the document data to a file
// lpszPathName = path name where to save document file
// if lpszPathName is NULL then the user will be prompted (SaveAs)
// note: lpszPathName can be different than 'm_strPathName'
// if 'bReplace' is TRUE will change file name if successful (SaveAs)
// if 'bReplace' is FALSE will not change path name (SaveCopyAs)
{
CString newName = lpszPathName;
if (newName.IsEmpty())
{
// ...
if (!AfxGetApp()->DoPromptFileName(newName,
// ...https://stackoverflow.com/questions/62139285
复制相似问题