2009-08-20 8 views
5

Lorsque j'essaie d'instancier un objet CFileDialog, il affiche à la fois les dossiers et les fichiers. Comment créez-vous un CFileDialog qui recherche les dossiers seuls?CFileDialog :: Parcourir les dossiers

Merci ...

Répondre

4

A partir de Vista, il est recommandé d'utiliser IFileDialog avec l'option 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

Démarrage Visual Studio 2010 cela ne fonctionnera pas. Utilisez plutôt CFolderPickerDialog (https://msdn.microsoft.com/ru-ru/library/dd795962%28v=vs.120%29.aspx) – BlackBada

0

me semble la réponse vous demandez est à l'intérieur du code de

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

du

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

fichier.

Si vous n'avez pas accès au code, je posterai la partie essentielle:

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

Comme vous le voyez, Microsoft lui-même n'utilise pas la classe CFileDialog lorsque veut ouvrir une boîte de dialogue pour la cueillette Dossiers.

Pour utiliser le code comme ça, votre classe d'application doit être dérivée de CWinAppEx, non CWinApp

4

Il est très simple, vraiment. Utilisez CFolderPickerDialog qui est dérivé de la classe CFileDialog!

+1

Cette classe existe à partir de Visual Studio 2010. nous venons de passer de VS2008 à VS2013 et IFileOpenDialog :: SetOptions (FOS_PICKFOLDERS) ont étonnamment cessé de fonctionner. À mon humble avis, c'est contre la rétrocompatibilité. – BlackBada

0

Comme quelqu'un l'a mentionné, utilisez CFolderPickerDialog qui fonctionne très bien. Je voudrais vous donner l'exemple comment l'utiliser en particulier lors de l'utilisation du drapeau de sélection multiple:

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

à partir de windows vista, vous pouvez utiliser le 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(); 
    } 
    } 
Questions connexes