由于CFileDialog只能选文件,现在只想要目录怎么做?

解决方案 »

  1.   

    // TODO: Add your control notification handler code here
       BROWSEINFO bi;
       TCHAR szDir[MAX_PATH];
       LPITEMIDLIST pidl;
       LPMALLOC pMalloc;   if (SUCCEEDED(SHGetMalloc(&pMalloc)))
       {
       ZeroMemory(&bi, sizeof(bi));    bi.hwndOwner = this->m_hWnd;
       bi.pszDisplayName = 0;
       bi.pidlRoot = 0;
       bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_VALIDATE ;    
          pidl = SHBrowseForFolder(&bi);
          if (pidl)
          {
             if (SHGetPathFromIDList(pidl,szDir))
             {
     m_strDirectory = szDir;
                 UpdateData(FALSE);
             }
               pMalloc->Free(pidl);
     pMalloc->Release();
          }//
      else
      {
      }//end
       }
      

  2.   

    void CSelDirDlg::OnSeldir() 
    {
    CString strResult =""; 
        LPMALLOC lpMalloc;  // pointer to IMalloc 
        if (::SHGetMalloc(&lpMalloc) != NOERROR) 

    AfxMessageBox("Path operation error!"); 
    return ; 

        char szDisplayName[_MAX_PATH]; 
        char szBuffer[_MAX_PATH]; 
        BROWSEINFO browseInfo; 
        browseInfo.hwndOwner = this->m_hWnd; 
        // set root at Desktop 
        browseInfo.pidlRoot = NULL; 
        browseInfo.pszDisplayName = szDisplayName; 
        browseInfo.lpszTitle = "请选择分类数据路径";  // Dialog title 
        browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS; 
        browseInfo.lpfn = NULL;      // not used 
        browseInfo.lParam = 0;      // not used 
        LPITEMIDLIST lpItemIDList; 
        if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) 
            != NULL) 
        { 
            // Get the path of the selected folder from the    item ID list. 
            if (::SHGetPathFromIDList(lpItemIDList, szBuffer)) 
            { 
                // At this point, szBuffer contains the path the user chose. 
                if (szBuffer[0] == '\0') 
                { 
                    // SHGetPathFromIDList failed, or SHBrowseForFolder failed. 
                    AfxMessageBox("Fail to get directory!", 
                        MB_ICONSTOP|MB_OK); 
                    return ; 
                } 
                // We have a path in szBuffer! Return it. 
                strResult = szBuffer; 
            } 
            else 
            { 
                // The thing referred to by lpItemIDList 
                // might not have been a file system object. 
                // For whatever reason, SHGetPathFromIDList didn't work! 
                AfxMessageBox("Fail to get directory!", 
                    MB_ICONSTOP|MB_OK); 
                return ; 
            } 
            lpMalloc->Free(lpItemIDList); 
            lpMalloc->Release(); 
        } 
    CString strMsg;
    strMsg.Format("选择目录:%s",strResult);
    AfxMessageBox(strMsg);
    UpdateData(FALSE); 
    }