2009-08-20 32 views
5

Khi tôi cố gắng instantiat một đối tượng CFileDialog nó cho thấy cả các thư mục và tập tin. Làm thế nào để bạn tạo một CFileDialog mà duyệt cho các thư mục một mình?CFileDialog :: Duyệt các thư mục

Cảm ơn ...

Trả lời

4

Bắt đầu từ Vista nó khuyến khích sử dụng IFileDialog với các tùy chọn 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(); 
+0

Bắt đầu Visual Studio 2010 điều này sẽ không hoạt động. Sử dụng CFolderPickerDialog thay vào đó (https://msdn.microsoft.com/ru-ru/library/dd795962%28v=vs.120%29.aspx) – BlackBada

0

Dường như với tôi câu trả lời bạn đang yêu cầu nằm trong mã số của

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

của

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

tệp.

Nếu bạn không có quyền truy cập vào mã, tôi sẽ đăng một phần thiết yếu của nó:

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

Như bạn thấy, bản thân Microsoft không sử dụng lớp CFileDialog khi muốn mở một hộp thoại để chọn thư mục.

Đối với sử dụng mã như thế, lớp ứng dụng của bạn phải được bắt nguồn từ CWinAppEx, không CWinApp

4

Nó rất đơn giản, thực sự. Sử dụng CFolderPickerDialog có nguồn gốc từ lớp CFileDialog!

+1

Lớp này tồn tại bắt đầu từ Visual Studio 2010. chúng tôi vừa chuyển từ VS2008 sang VS2013, và IFileOpenDialog :: SetOptions (FOS_PICKFOLDERS) bất ngờ ngừng hoạt động. IMHO chống lại khả năng tương thích ngược. – BlackBada

0

Giống như ai đó đã đề cập, hãy sử dụng CFolderPickerDialog hoạt động tốt. Tôi muốn cung cấp cho bạn ví dụ về cách sử dụng nó đặc biệt khi sử dụng cờ đa chọn:

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

     } 
    } 
1

bắt đầu từ windows vista, bạn có thể sử dụng 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(); 
    } 
    } 
Các vấn đề liên quan