Re: Easiest way to set the File Open default directory?
Sonu wrote:
Try this:
TCHAR *strFilter = _T("Text files(*.txt)|*.txt||");
Data.Format( _T("%s%s"), mod_dir, _T("\\*.txt") );
CFileDialog ChartOpen ( TRUE, _T("*.txt"), Data, 0, strFilter );
ChartOpen.m_ofn.Flags = OFN_FILEMUSTEXIST |
OFN_HIDEREADONLY |
OFN_NONETWORKBUTTON |
OFN_OVERWRITEPROMPT |
OFN_PATHMUSTEXIST |
OFN_EXPLORER |
OFN_ENABLEHOOK ;
if( ChartOpen.DoModal() == IDOK )
{
...
}
I have all this in CMainFrm class but I am sure it shouldn't matter where
you put it!
I'm not sure if that'll work, but I came up with my own solution, and I
figure that, in the interest of helping anyone in the future who might
have this problem, I'll offer up my solution. This is in Visual C++
..NET and I put all of the functions directly into the main
application's class (in my case, CCoilApp).
I'm making a function called OnFileOpen(). First, I have to set it up
to correctly receive messages by adding it to the message map (in
CCoilApp):
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
Now I add a few headers into CCoilApp for stuff that I'll need:
public:
afx_msg void OnFileOpen();
BOOL DoPromptOpenFile(CString& fileName, UINT nIDSTitle, DWORD lFlags,
BOOL bOpenFileDialog, CDocTemplate* pTemplate);
bool firstOpen;
I edit the CCoilApp constructor to set firstOpen to true. Now, for the
two functions that do all of the work:
void CCoilApp::OnFileOpen()
{
if (!firstOpen) {
CWinApp::OnFileOpen();
}
else {
firstOpen = false;
// prompt the user (with all document templates)
CString newName;
if (!DoPromptOpenFile(newName, AFX_IDS_OPENFILE, OFN_HIDEREADONLY |
OFN_FILEMUSTEXIST, TRUE, NULL))
return; // open cancelled
AfxGetApp()->OpenDocumentFile(newName);
}
// if returns NULL, the user has already been alerted
}
BOOL CCoilApp::DoPromptOpenFile(CString& fileName, UINT nIDSTitle,
DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate)
{
CFileDialog dlgFile(bOpenFileDialog, NULL, NULL, OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT, NULL, NULL, 0);
CString title;
VERIFY(title.LoadString(nIDSTitle));
dlgFile.m_ofn.Flags |= lFlags;
CString strFilter;
CString strDefault;
// append the "*.*" all files filter
CString allFilter;
VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
strFilter += allFilter;
strFilter += (TCHAR)'\0'; // next string please
strFilter += _T("*.*");
strFilter += (TCHAR)'\0'; // last string
dlgFile.m_ofn.nMaxCustFilter++;
TCHAR file[_MAX_PATH];
GetModuleFileName( NULL, file, _MAX_PATH );
CString mod_dir = file;
mod_dir = mod_dir.Mid( 0, mod_dir.ReverseFind( '\\' ) );
#if _DEBUG
mod_dir = mod_dir.Mid( 0, mod_dir.ReverseFind( '\\' ) );
#endif
mod_dir += "\\HX";
dlgFile.m_ofn.lpstrFilter = strFilter;
dlgFile.m_ofn.lpstrTitle = title;
dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
dlgFile.m_ofn.lpstrInitialDir = mod_dir;
INT_PTR nResult = dlgFile.DoModal();
fileName.ReleaseBuffer();
return nResult == IDOK;
}
The way it works is rather simple. By copying over and slightly
modifying the code from the MFC classes that handle file loading I get
the desired functionality of setting the open directory to the
program's location (plus HX\ where the data is saved) when it is first
run, but on subsequent File->Opens, the boolean is false, so the
default method is called, thus, the user won't have to keep navigating
over and over to a different directory.