Initial directory is not working for CFileDialog

走远了吗. 提交于 2019-12-10 14:56:20

问题


I am using CFileDialog, I have set the initial path like below , as shown in the code. It's not working . Correct me if I made a mistake.

   CFileDialog* filedlg = new CFileDialog(TRUE,(LPCTSTR)NULL ,  (LPCTSTR)NULL , OFN_HIDEREADONLY| OFN_ENABLESIZING , (LPCTSTR)NULL , FromHandle (hImgDlg) ,0 , FALSE  );

   filedlg ->m_ofn.lpstrInitialDir = "C:\\" ;

   if ( filedlg ->DoModal() == IDOK )
   {
       /***  do somthing here *****/
   }

回答1:


If you see the reference for the OPENFILENAME structure, you will see that for the lpstrInitialDir field it states that:

If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory.

This means that the lpstrInitialDir field can really only be used the first time you use the dialog in a program. The rest of the time it will use the last directory selected by the user.




回答2:


If you set the filename location, you can get the dialog to open to a specific location. I would only use this if you really needed the folder location to open or if you have a default filename that you use.

CFileDialog* filedlg = new CFileDialog(TRUE, (LPCTSTR)NULL,  (LPCTSTR)_T("C:\\MyFolder\\DefaultFileName.ext"), OFN_HIDEREADONLY | OFN_ENABLESIZING, (LPCTSTR)NULL, FromHandle (hImgDlg), 0, FALSE);

or you could use the Windows function GetModuleFileName:

CString csAppFolder;
TCHAR szPath[MAX_PATH]; 

// form the path to where we want to store the file
if (GetModuleFileName(NULL, szPath, MAX_PATH))
{
    PathRemoveFileSpec(szPath);
    csAppFolder = szPath;
}

CFileDialog* filedlg = new CFileDialog(TRUE, (LPCTSTR)NULL, (LPCTSTR)(csAppFolder + _T("\\DefaultFileName.ext")), OFN_HIDEREADONLY | OFN_ENABLESIZING, (LPCTSTR)NULL, FromHandle (hImgDlg), 0, FALSE);



回答3:


Two options: 1. Old-fashioned dialog style, specifying OFN::lpstrInitialDir

CFileLatinDialog dlg (TRUE, "", "" /*lpszFileName */,
   OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
   "All Files(*.*)|*.*||", this, 0,
   FALSE /*bVistaStyle*/);
dlg.m_ofn.lpstrInitialDir = "C:\\Models\\";
  1. Vista style dialog, specifying lpszFileName parameter
CFileLatinDialog dlg (TRUE, "", "C:\\Models\\" /*lpszFileName */,
   OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
   "All Files(*.*)|*.*||", this);


来源:https://stackoverflow.com/questions/16164637/initial-directory-is-not-working-for-cfiledialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!