的CFileDialog ::浏览文件夹(CFileDialog :: Browse folders

2019-07-02 11:45发布

当我尝试实例化一个CFileDialog对象就说明这两个文件夹和文件。 如何创建一个CFileDialog是浏览单独的文件夹?

Answer 1:

你不能做到这一点CFileDialog

要么你将使用的SHBrowseForFolder函数或它的包装,
像CFolderDialog -选择文件夹。



Answer 2:

这是非常简单的,真的。

使用CFolderPickerDialog这是从类派生CFileDialog



Answer 3:

从Vista开始它建议使用IFileDialog与FOS_PICKFOLDERS选项( 请参阅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();


Answer 4:

就像有人提到,使用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);

        }
    }


Answer 5:

从Windows Vista开始,您可以使用通用项对话框 。

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


Answer 6:

在我看来,你问的答案是里面的代码

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

正如你看到的,微软自己想打开一个对话框,选择文件夹时,不使用CFileDialog类。

对于使用这样的代码,你的应用类必须从CWinAppEx,不CWinApp的派生



文章来源: CFileDialog :: Browse folders