Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. MFC: How to change the default filename in the Save As dialog at runtime?

MFC: How to change the default filename in the Save As dialog at runtime?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestionc++
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Davex_
    wrote on last edited by
    #1

    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

    B R 2 Replies Last reply
    0
    • D Davex_

      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

      B Offline
      B Offline
      basementman
      wrote on last edited by
      #2

      Call CDocument's SetPathName() or SetTitle prior to showing the FileSave Dlg.  onwards and upwards...

      1 Reply Last reply
      0
      • D Davex_

        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

        R Offline
        R Offline
        Roger Allen
        wrote on last edited by
        #3

        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...

        D 1 Reply Last reply
        0
        • R Roger Allen

          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...

          D Offline
          D Offline
          Davex_
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups