Re: OnFileOpen, OnFileSaveAs remembers last directory

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Jiangsheng[Microsoft MVP of VC++] (sheng_jiang_at_hotmail.com.discuss)
Date: 05/27/04

  • Next message: Andy Mortimer [MS]: "RE: Losing main form content when switching screens"
    Date: Thu, 27 May 2004 19:09:05 +0800
    
    

    LRESULT OnClickedOpen(WORD wNotifyCode, WORD wID, HWND hWndCtl,
                              BOOL& bHandled)
        {
            USES_CONVERSION;
            OPENFILENAME ofn;
            ZeroMemory(&ofn, sizeof(OPENFILENAME));
            ofn.lStructSize = sizeof(OPENFILENAME);

            TCHAR szFile[MAX_PATH] = {0};
            ofn.lpstrFile = szFile;
            ofn.nMaxFile = MAX_PATH;

            ofn.lpstrInitialDir = OLE2T(m_strPath);

            ofn.hwndOwner = ::GetParent(m_hwndEdit);

            // Sets the filter string replacing pipes with nulls
            TCHAR szFilter[MAX_PATH];
            lstrcpy(szFilter, OLE2T(m_strFilter));
            LPTSTR psz = szFilter;
            int i=0;
            while(psz[i] != 0) {
                if (psz[i]=='|')
                    psz[i]=0;
                i++;
            }
            ofn.lpstrFilter = szFilter;

            if (GetOpenFileName(&ofn)) {
                m_strPath.Empty();
                m_strPath.Attach(T2OLE(ofn.lpstrFile));
                ::SetWindowText(m_hwndEdit, ofn.lpstrFile);
            }
            return 0;
        }

    "Anders Eriksson" <anders.eriksson@morateknikutveckling.se> дÈëÓʼþ
    news:2oaj9fhkqhc5.dlg@morateknikutveckling.se...
    > Hello!
    >
    > I have all documents for my application in CSIDL_COMMON_APPDATA\MyApp[1]
    > and I have created CMyAppDoc::OnFileOpen() and CMyAppDoc::OnFileSaveAs()
    > and in them I SetCurrentDirectory() like this:
    >
    > SHGetFolderPath( NULL, CSIDL_COMMON_APPDATA, NULL,0, szAppData );
    > filepath = szAppData;
    > filepath += _T("\\MyApp\\");
    > SetCurrentDirectory(filepath);
    >
    > Then in OnFileOpen I call:
    >
    > BOOL brc = AfxGetApp()->DoPromptFileName(filepath1,AFX_IDS_APP_TITLE
    > ,OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE, NULL);
    >
    > if(brc)
    > AfxGetApp()->OpenDocumentFile(filepath1);
    >
    > and in OnFileSaveAs I call:
    >
    > DoSave(NULL);
    >
    > The bad thing is that if the user has opened a file in an another
    > directory, e.g. My Documents, then this will be remembered and the next
    > time the user opens a document he will be directed to My Documents instead
    > om CSIDL_COMMON_APPDATA\MyApp
    >
    > How to I make MyApp to always use CSIDL_COMMON_APPDATA\MyApp as the data
    > directory??
    >
    > // Anders
    >
    > --
    > [1] The reason for using CSIDL_COMMON_APPDATA instead of
    > CSIDL_COMMON_DOCUMENTS is that I can't get Windows Installer to access the
    > CSIDL_COMMON_DOCUMENTS directory...
    >
    > Crossposted to: microsoft.public.vc.mfc.docview,microsoft.public.vc.mfc
    > Replies will go to: microsoft.public.vc.mfc.docview


  • Next message: Andy Mortimer [MS]: "RE: Losing main form content when switching screens"

    Relevant Pages

    • OnFileOpen, OnFileSaveAs remembers last directory
      ... and in them I SetCurrentDirectory() like this: ... and in OnFileSaveAs I call: ... time the user opens a document he will be directed to My Documents instead ... Replies will go to: microsoft.public.vc.mfc.docview ...
      (microsoft.public.vc.mfc.docview)
    • OnFileOpen, OnFileSaveAs remembers last directory
      ... and in them I SetCurrentDirectory() like this: ... and in OnFileSaveAs I call: ... time the user opens a document he will be directed to My Documents instead ... Replies will go to: microsoft.public.vc.mfc.docview ...
      (microsoft.public.vc.mfc)