MFC: How to change the default filename in the Save As dialog at runtime?
-
Hi, Here's what I'm trying to do in my MFC application. Like MS WORD do when you save a document for the first time, you click Save and Word takes the first line of your text and wants to save your document to "My first line of text.doc". Me, I want to create my default file name with the current date, example: today is March 15, then when I click Save (or save as) I would like to see "March15.las". The default save name is found in the string table under the IDR_MAINFRAME (ID 128). My IDR_MAINFRAME STRING : LaserPlusBeta1\ndefaultsavename\nLaserPlusBeta1\nLaserPlusBeta1 Files (*.las)\n.las\nLaserPlusBeta1.Document\nLaserPlusBeta1.Document Is it possible to modify the caption of IDR_MAINFRAME at runtime? If not, how can I generate my own string every time I save and associate an ID to it? Because the CSingleDocTemplate takes a UINT as first parameter for the IDR_MAINFRAME string. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CLaserPlusBeta1Doc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CLaserPlusBeta1View)); Thank you Dave
-
Hi, Here's what I'm trying to do in my MFC application. Like MS WORD do when you save a document for the first time, you click Save and Word takes the first line of your text and wants to save your document to "My first line of text.doc". Me, I want to create my default file name with the current date, example: today is March 15, then when I click Save (or save as) I would like to see "March15.las". The default save name is found in the string table under the IDR_MAINFRAME (ID 128). My IDR_MAINFRAME STRING : LaserPlusBeta1\ndefaultsavename\nLaserPlusBeta1\nLaserPlusBeta1 Files (*.las)\n.las\nLaserPlusBeta1.Document\nLaserPlusBeta1.Document Is it possible to modify the caption of IDR_MAINFRAME at runtime? If not, how can I generate my own string every time I save and associate an ID to it? Because the CSingleDocTemplate takes a UINT as first parameter for the IDR_MAINFRAME string. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CLaserPlusBeta1Doc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CLaserPlusBeta1View)); Thank you Dave
Call CDocument's SetPathName() or SetTitle prior to showing the FileSave Dlg. onwards and upwards...
-
Hi, Here's what I'm trying to do in my MFC application. Like MS WORD do when you save a document for the first time, you click Save and Word takes the first line of your text and wants to save your document to "My first line of text.doc". Me, I want to create my default file name with the current date, example: today is March 15, then when I click Save (or save as) I would like to see "March15.las". The default save name is found in the string table under the IDR_MAINFRAME (ID 128). My IDR_MAINFRAME STRING : LaserPlusBeta1\ndefaultsavename\nLaserPlusBeta1\nLaserPlusBeta1 Files (*.las)\n.las\nLaserPlusBeta1.Document\nLaserPlusBeta1.Document Is it possible to modify the caption of IDR_MAINFRAME at runtime? If not, how can I generate my own string every time I save and associate an ID to it? Because the CSingleDocTemplate takes a UINT as first parameter for the IDR_MAINFRAME string. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CLaserPlusBeta1Doc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CLaserPlusBeta1View)); Thank you Dave
If you take alook at the default MFC implementation of CDocument::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())
{
**CDocTemplate* pTemplate = GetDocTemplate();
ASSERT(pTemplate != NULL);newName = m\_strPathName; if (bReplace && newName.IsEmpty()) { newName = m\_strTitle; // check for dubious filename int iBad = newName.FindOneOf(\_T(" #%;/\\\\")); if (iBad != -1) newName.ReleaseBuffer(iBad); // append the default suffix if there is one CString strExt; if (pTemplate->GetDocString(strExt, CDocTemplate::filterExt) && !strExt.IsEmpty()) { ASSERT(strExt\[0\] == '.'); newName += strExt; } }** if (!AfxGetApp()->DoPromptFileName(newName, bReplace ? AFX\_IDS\_SAVEFILE : AFX\_IDS\_SAVEFILECOPY, OFN\_HIDEREADONLY | OFN\_PATHMUSTEXIST, FALSE, pTemplate)) return FALSE; // don't even attempt to save } CWaitCursor wait; if (!OnSaveDocument(newName)) { if (lpszPathName == NULL) { // be sure to delete the file TRY { CFile::Remove(newName); } CATCH\_ALL(e) { TRACE0("Warning: failed to delete file after failed SaveAs.\\n"); DELETE\_EXCEPTION(e); } END\_CATCH\_ALL } return FALSE; } // reset the title and change the document name if (bReplace) SetPathName(newName); return TRUE; // success
}
If you were to override this function in your MFC document copy/paste the above and replace the bold section with your own code to determine the default filename, it should do what you need. Roger Allen - Sonork 100.10016 Roger Wright: Remember to buckle up, please, and encourage your friends to do the same. It's not just about saving your life, but saving the quality of life for those you may leave behind...
-
If you take alook at the default MFC implementation of CDocument::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())
{
**CDocTemplate* pTemplate = GetDocTemplate();
ASSERT(pTemplate != NULL);newName = m\_strPathName; if (bReplace && newName.IsEmpty()) { newName = m\_strTitle; // check for dubious filename int iBad = newName.FindOneOf(\_T(" #%;/\\\\")); if (iBad != -1) newName.ReleaseBuffer(iBad); // append the default suffix if there is one CString strExt; if (pTemplate->GetDocString(strExt, CDocTemplate::filterExt) && !strExt.IsEmpty()) { ASSERT(strExt\[0\] == '.'); newName += strExt; } }** if (!AfxGetApp()->DoPromptFileName(newName, bReplace ? AFX\_IDS\_SAVEFILE : AFX\_IDS\_SAVEFILECOPY, OFN\_HIDEREADONLY | OFN\_PATHMUSTEXIST, FALSE, pTemplate)) return FALSE; // don't even attempt to save } CWaitCursor wait; if (!OnSaveDocument(newName)) { if (lpszPathName == NULL) { // be sure to delete the file TRY { CFile::Remove(newName); } CATCH\_ALL(e) { TRACE0("Warning: failed to delete file after failed SaveAs.\\n"); DELETE\_EXCEPTION(e); } END\_CATCH\_ALL } return FALSE; } // reset the title and change the document name if (bReplace) SetPathName(newName); return TRUE; // success
}
If you were to override this function in your MFC document copy/paste the above and replace the bold section with your own code to determine the default filename, it should do what you need. Roger Allen - Sonork 100.10016 Roger Wright: Remember to buckle up, please, and encourage your friends to do the same. It's not just about saving your life, but saving the quality of life for those you may leave behind...
Thanks for your answer, But I did what basementman suggested: I'm using SetTitle() and it's working fine. Here's where I put the function, in my CLaserPlusBeta1Doc.cpp: BOOL CLaserPlusBeta1Doc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; // TODO: add reinitialization code here // (SDI documents will reuse this document) CDocument::SetTitle("MyTitle"); return TRUE; } I don't need to specify a path name, so it's easier this way. Dave