CFileDialog ::浏览文件夹

时间:2009-08-20 08:31:00

标签: mfc visual-c++

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

7 个答案:

答案 0 :(得分:11)

您无法使用CFileDialog执行此操作。

您可以使用SHBrowseForFolder Function或包装器,例如CFolderDialog - Selecting Folders.

答案 1 :(得分:10)

这很简单,真的。

使用CFolderPickerDialog,它来自班级CFileDialog

答案 2 :(得分:5)

从Vista开始,建议将IFileDialog与FOS_PICKFOLDERS选项(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();

答案 3 :(得分: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);

        }
    }

答案 4 :(得分: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();
  }
  }

答案 5 :(得分: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

答案 6 :(得分:0)

实际上有一种方法可以做到这一点 - 我在 codeguru: "Selected files and folders in CFileDialog"

中找到了它

如果您愿意自己实现 CFileDialog,例如: class CMyFileDialog : public CFileDialog

您可以添加以下代码,它应该可以工作(与 codeguru example 略有不同):

// This code overrides the OnNotify message of the CFileDialog
// and catches the CDN_SELCHANGE, this way you can also do 
// something with the selected folders.
BOOL CMyFileDialog::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
    NMHDR* pNotificationParam = (NMHDR*)lParam;

    // Check that we got to the selection change notification.
    int code = pNotificationParam->code;
    if (code == CDN_SELCHANGE)
    {
        CStringArray theSelection;

        GetListControllSelectedItems(theSelection);

        // Do as you want with theSelection.
    }

    return CFileDialog::OnNotify(wParam, lParam, pResult);
}

// The following Code is accessing the selection in the CFileDialog
// and filling a string array with the selected names
BOOL CMyFileDialog::GetListControllSelectedItems(CStringArray& selectedItemNames)
{
    BOOL rc = FALSE;
    // Get the list control of the file dialog.
    CWnd* pParentWnd = GetParent();
    CWnd* pListControlWnd = pParentWnd->GetDlgItem(lst2);
    if (pListControlWnd) {

        // Get the selection from the list control.
        CListCtrl* pListCtrl = (CListCtrl*)(pListControlWnd->GetDlgItem(1));

        UINT selectionCount = pListCtrl->GetSelectedCount();

        // When there are items selected.
        if (selectionCount) {
            rc = TRUE;
            selectedItemNames.RemoveAll();
            POSITION itemPos = pListCtrl->GetFirstSelectedItemPosition();
            while (itemPos != NULL)
            {
                int itemNum = pListCtrl->GetNextSelectedItem(itemPos);
                CString currentItemName = pListCtrl->GetItemText(itemNum, 0);
                selectedItemNames.Add(currentItemName);
            }
        }
    }
    
    return rc;
}

注意:在 CFileDialog::OnFileNameChange of the Microsoft MFC documentation 中,他们确实暗示了这个解决方案,但没有详细说明。

我在非常旧的遗留代码中遇到了一个问题,我有一个自定义的文件对话框,实际上需要保存一个文件夹!!!

经过二十二年的艰辛和痛苦,我的代码现在完成了......