CFileDialog ::浏览文件夹

问题描述 投票:7回答:6

当我尝试实例化CFileDialog对象时,它会显示文件夹和文件。你如何创建一个单独浏览文件夹的CFileDialog

mfc visual-c++
6个回答
11
投票

你不能用CFileDialog做到这一点。 要么你会使用SHBrowseForFolder Function或它的包装, 像CFolderDialog - Selecting Folders.


10
投票

真的很简单。

使用源自CFolderPickerDialog类的CFileDialog


5
投票

从Vista开始,建议使用带有FOS_PICKFOLDERS选项的IFileDialog(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();

2
投票

就像有人提到的那样,使用效果很好的CFolderPickerDialog。我想举例说明如何使用它,尤其是在使用多选标志时:

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);

        }
    }

2
投票

从windows vista开始,你可以使用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
投票

在我看来,你要求的答案是在代码中

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

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

文件。

如果您无法访问代码,我将发布它的基本部分:

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();
    }
}

如您所见,当想要打开用于拾取文件夹的对话框时,Microsoft本身不使用Cfiledialog类。

对于使用这样的代码,您的应用程序类必须从CWinAppEx派生,而不是CWinApp

© www.soinside.com 2019 - 2024. All rights reserved.