显示文件夹对话框,返回选中文件夹的路径

类别:VC语言 点击:0 评论:0 推荐:

//显示文件夹对话框,返回选中文件夹的路径
CString BrowseForFolder( HWND hWnd )
{
     TCHAR szTitle[] = _T("Select a folder"); 
     TCHAR  szDisplayName[MAX_PATH] = _T("");
     TCHAR  szPath[MAX_PATH] = _T("");
     BROWSEINFO bi;

     bi.hwndOwner = hWnd;
     bi.pidlRoot = NULL;
     bi.lpszTitle = szTitle;
     bi.pszDisplayName = szDisplayName;
     bi.ulFlags = BIF_RETURNONLYFSDIRS;
     bi.lpfn  = NULL;
     bi.lParam = 0;

     LPITEMIDLIST pItemIDList = SHBrowseForFolder( &bi );
     if( pItemIDList )
     {
              SHGetPathFromIDList(pItemIDList,szPath) ;
              
              IMalloc *pMalloc;
              if( SHGetMalloc( &pMalloc ) != NOERROR )
              {
                       TRACE( "Failed to get pointer to shells task allocator" ) ;
                       return szPath;
              }
              pMalloc->Free( pItemIDList );
              if( pMalloc )
                   pMalloc->Release();
       }
       return szPath;
}

本文地址:http://com.8s8s.com/it/it438.htm