Re: Easiest way to set the File Open default directory?
Cyde Weys wrote:
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;
}
A quick addendum: I was able to get this function to be much shorter
and compact while providing more useful functionality for my program as
follows. I basically took out a bunch of unnecessary stuff (not shown:
the corresponding edits to the header file and the function call in
OnFileOpen() to reduce the number of parameters to what's given here).
BOOL CCoilApp::DoPromptOpenFile(CString& fileName, DWORD lFlags, BOOL
bOpenFileDialog)
{
CFileDialog dlgFile(bOpenFileDialog, NULL, NULL, OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT, NULL, NULL, 0);
dlgFile.m_ofn.Flags |= lFlags;
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 = "Coil files (*.dat)\0*.dat\0All files
(*.*)\0*.*\0";
dlgFile.m_ofn.lpstrTitle = "Open coil file";
dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
dlgFile.m_ofn.lpstrInitialDir = mod_dir;
INT_PTR nResult = dlgFile.DoModal();
fileName.ReleaseBuffer();
return nResult == IDOK;
}