CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 1999
    Location
    UK (South)
    Posts
    93

    Setting Up a default path for saving and Opening files in the MFC Framework

    I have an MFC SDI application. I have stored a Key in the Registry that contains the default directory that is used for storing files created and read from the application. I have no problems extracting the key from the registry within my application.

    I would like to no what the best practice within the MFC Doc/View Framework for implementing a default directory.

    At the moment I have Over Ridden


    void CWinApp::OnFileOpen()




    And


    BOOL CDocument:oSave(LPCTSTR lpszPathName, BOOL bReplace)




    The DoSave implementation is a little messy and I am concerned about its robustness. Is there a suggested way to do this?

    Simon Pettman
    All answers rated!


  2. #2
    Join Date
    Jul 2001
    Location
    Austria
    Posts
    62

    Re: Setting Up a default path for saving and Opening files in the MFC Framework

    Hi,

    I think a quite simple way is to override the OnFileOpen, OnFileSave and OnFileSaveAs functions.
    I have done this and it's not really difficult.

    I have implemented it like the following ...


    void CSDITestDoc::OnFileOpen()
    {
    CString strFileName;
    CString strCurrentDir;

    // Create a file open dialog
    CFileDialog fdOpen(TRUE, NULL, NULL, OFN_FILEMUSTEXIST,
    "Text files (*.txt)|*.txt|All Files (*.*)|*.*||");

    // Do your stuff here ...
    strCurrentDir = "C:\\Temp";

    // Set current directory for file open dialog
    fdOpen.m_ofn.lpstrInitialDir = strCurrentDir;

    // Open dialog
    if (fdOpen.DoModal() == IDOK)
    {
    // Get file name
    strFileName = fdOpen.GetPathName();

    // Set new file name -> you have to do this !
    SetPathName(strFileName);

    // Open document
    OnOpenDocument(strFileName);
    }
    }

    void CSDITestDoc::OnFileSave()
    {
    CString strFileName = GetPathName();

    // Verify if a file name is specified
    if (strFileName.IsEmpty())
    {
    // Call file save as function
    OnFileSaveAs();
    }
    else
    {
    // Save document
    OnSaveDocument(strFileName);
    SetModifiedFlag(FALSE);
    }
    }

    void CSDITestDoc::OnFileSaveAs()
    {
    CString strFileName;
    CString strCurrentDir;

    // Create a file save as dialog
    CFileDialog fdSaveAs(FALSE, NULL, NULL, OFN_OVERWRITEPROMPT,
    "Text files (*.txt)|*.txt|All Files (*.*)|*.*||");

    // Do your stuff here ...
    strCurrentDir = "C:\\Temp";

    // Set current directory for file save as dialog
    fdSaveAs.m_ofn.lpstrInitialDir = strCurrentDir;

    // Open dialog
    if (fdSaveAs.DoModal() == IDOK)
    {
    // Get file name
    strFileName = fdSaveAs.GetPathName();

    // Set new file name -> you have to do this !
    SetPathName(strFileName);

    // Save document
    OnSaveDocument(strFileName);
    SetModifiedFlag(FALSE);
    }
    }




    Hope it helps :-)
    AcidBurn


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured