Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CFileDialog :: Browse folders

Tags:

visual-c++

mfc

When I try to instantiate a CFileDialog object it shows both the folders and files. How do you create a CFileDialog that browses for folders alone?

like image 223
Owen Avatar asked Aug 20 '09 08:08

Owen


5 Answers

Seems to me the answer you are asking for is inside the code of

CMFCPropertyGridFileProperty::OnClickButton(CPoint /*point*/)

of the

<Your Visual Studio installation folder>\VC\atlmfc\src\mfc\afxpropertygridctrl.cpp

file.

If you do not have access to the code, I will post the essential part of it:

CString strPath = m_varValue.bstrVal;
BOOL bUpdate = FALSE;

if (m_bIsFolder)
{
    if (afxShellManager == NULL)
    {
        CWinAppEx* pApp = DYNAMIC_DOWNCAST(CWinAppEx, AfxGetApp());
        if (pApp != NULL)
        {
            pApp->InitShellManager();
        }
    }

    if (afxShellManager == NULL)
    {
        ASSERT(FALSE);
    }
    else
    {
        bUpdate = afxShellManager->BrowseForFolder(strPath, m_pWndList, strPath);
    }
}
else
{
    CFileDialog dlg(m_bOpenFileDialog, m_strDefExt, strPath, m_dwFileOpenFlags, m_strFilter, m_pWndList);
    if (dlg.DoModal() == IDOK)
    {
        bUpdate = TRUE;
        strPath = dlg.GetPathName();
    }
}

As you see, Microsoft itself does not use the Cfiledialog class when wants to open a dialog for picking folders.

For using code like that, your application class MUST be derived from CWinAppEx, not CWinApp

like image 130
sergiol Avatar answered Sep 20 '22 14:09

sergiol


It is very simple, really.

Use CFolderPickerDialog which is derived from the class CFileDialog!

like image 21
user5498719 Avatar answered Oct 14 '22 03:10

user5498719


You can't do it with CFileDialog.

Either you will use SHBrowseForFolder Function or a wrapper for it,
like CFolderDialog - Selecting Folders.

like image 11
Nick Dandoulakis Avatar answered Oct 14 '22 03:10

Nick Dandoulakis


Starting from Vista it's recommended to use IFileDialog with the FOS_PICKFOLDERS option (see msdn):

CFileDialog od(TRUE/*bOpenFileDialog*/, NULL, NULL,
      OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT , NULL, NULL, 0,
      TRUE/*bVistaStyle*/);
   IFileOpenDialog * openDlgPtr = od.GetIFileOpenDialog();
   if ( openDlgPtr != NULL )
   {
      openDlgPtr->SetOptions(FOS_PICKFOLDERS);
      openDlgPtr->Release();
   }

   od.DoModal();
like image 6
blackbada_cpp Avatar answered Oct 14 '22 03:10

blackbada_cpp


Like someone mentioned, use CFolderPickerDialog which works great. I would like to give you example how to use it especially when using the multi select flag:

CFolderPickerDialog folderPickerDialog(initialFolder, OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_ENABLESIZING, this,
        sizeof(OPENFILENAME));

    CString folderPath;

    if (folderPickerDialog.DoModal() == IDOK)
    {

        POSITION pos = folderPickerDialog.GetStartPosition();

        while (pos)
        {
            folderPath = folderPickerDialog.GetNextPathName(pos);

        }
    }
like image 6
Gautam Jain Avatar answered Oct 14 '22 01:10

Gautam Jain