CFileDialog :: Browse folders

后端 未结 6 1051
野性不改
野性不改 2020-12-17 22:39

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?

相关标签:
6条回答
  • 2020-12-17 23:12

    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);
    
            }
        }
    
    0 讨论(0)
  • 2020-12-17 23:16

    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

    0 讨论(0)
  • 2020-12-17 23:17

    It is very simple, really.

    Use CFolderPickerDialog which is derived from the class CFileDialog!

    0 讨论(0)
  • 2020-12-17 23:19

    starting from windows vista,you can use the Common Item Dialog .

    void CQiliRegrvDlg::OnBnClickedSelectDir()
    {
    HRESULT hr = S_OK;
    
    // Create a new common open file dialog.
    IFileOpenDialog *pfd = NULL;
    hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&pfd));
    if (SUCCEEDED(hr))
    {
        // Set the dialog as a folder picker.
        DWORD dwOptions;
        hr = pfd->GetOptions(&dwOptions);
        if (SUCCEEDED(hr))
        {
            hr = pfd->SetOptions(dwOptions | FOS_PICKFOLDERS);
        }
    
        // Set the title of the dialog.
        if (SUCCEEDED(hr))
        {
            hr = pfd->SetTitle(L"Folder");
        }
        // Show the open file dialog.
        if (SUCCEEDED(hr))
        {
            hr = pfd->Show(m_hWnd);
            if (SUCCEEDED(hr))
            {
                // Get the selection from the user.
                IShellItem *psiResult = NULL;
                hr = pfd->GetResult(&psiResult);
                if (SUCCEEDED(hr))
                {
                    PWSTR pszPath = NULL;
                    hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszPath);
                    if (SUCCEEDED(hr))
                    {
                        m_appDir = pszPath;
                        SetDlgItemText(IDC_STATIC, m_appDir);
                        CoTaskMemFree(pszPath);
                    }
                    psiResult->Release();
                }
            }
        }
    
        pfd->Release();
      }
      }
    
    0 讨论(0)
  • 2020-12-17 23:24

    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();
    
    0 讨论(0)
  • 2020-12-17 23:30

    You can't do it with CFileDialog.

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

    0 讨论(0)
提交回复
热议问题