请问可不可以不继承CFileDialog,实现从对话框中获得选定的目录路径,而不是文件路径

解决方案 »

  1.   

    SHBrowseForFolder Function--------------------------------------------------------------------------------Displays a dialog box enabling the user to select a Shell folder.SyntaxLPITEMIDLIST SHBrowseForFolder(          LPBROWSEINFO lpbi
    );
    Parameterslpbi
    [in, out] Pointer to a BROWSEINFO structure. On entry, this structure conveys information used to display the dialog box. On exit, it receives information concerning the selected folder.
    Return ValueReturns a pointer to an item identifier list (PIDL) specifying the location of the selected folder relative to the root of the namespace. If the user chooses the Cancel button in the dialog box, the return value is NULL. 
    It is possible that the PIDL returned is that of a folder shortcut rather than a folder. For a full discussion of this case, see the Res section.The display name of the object selected is returned in the buffer pointed to by the pszDisplayName member of the BROWSEINFO parameter structure.
      

  2.   

    Minimum DLL Version shell32.dll version 4.0 or later 
    Custom Implementation No 
    Header shlobj.h 
    Import library shell32.lib 
    Minimum operating systems Windows NT 4.0, Windows 95 For example:#include <shlobj.h>
    #pragma comment(lib,"shobj.lib")// Macros for interface casts
    #ifdef __cplusplus
    #define IID_PPV_ARG(IType, ppType) IID_##IType, reinterpret_cast(static_cast(ppType))
    #else
    #define IID_PPV_ARG(IType, ppType) &IID_##IType, (void**)(ppType)
    #endif// Retrieves the UIObject interface for the specified full PIDL
    STDAPI SHGetUIObjectFromFullPIDL(LPCITEMIDLIST pidl, HWND hwnd, REFIID riid, void **ppv)
    {
        LPCITEMIDLIST pidlChild;
        IShellFolder* psf;    *ppv = NULL;    HRESULT hr = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &psf), &pidlChild);
        if (SUCCEEDED(hr))
        {
            hr = psf->GetUIObjectOf(hwnd, 1, &pidlChild, riid, NULL, ppv);
            psf->Release();
        }
        return hr;
    }
     
    #define ILSkip(pidl, cb)       ((LPITEMIDLIST)(((BYTE*)(pidl))+cb))
    #define ILNext(pidl)           ILSkip(pidl, (pidl)->mkid.cb)
     
    HRESULT SHILClone(LPCITEMIDLIST pidl, LPITEMIDLIST *ppidl)
    {
        DWORD cbTotal = 0;    if (pidl) 
        {
            cbTotal += sizeof(pidl->mkid.cb);
            while (pidl->mkid.cb) 
            {
                cbTotal += pidl->mkid.cb;
                pidl = ILNext(pidl);
            }
        }
        
        *ppidl = (LPITEMIDLIST)CoTaskMemAlloc(cbTotal);
        
        if (*ppidl)
            CopyMemory(*ppidl, pidl, cbTotal);
     
        return  *ppidl ? S_OK: E_OUTOFMEMORY;
    }
     
    // Get the target PIDL for a folder PIDL. This deals with cases where a folder
    // is an alias to a real folder, folder shortcuts, etc.
    STDAPI SHGetTargetFolderIDList(LPCITEMIDLIST pidlFolder, LPITEMIDLIST *ppidl)
    {
        IShellLink *psl;

        *ppidl = NULL;
        
        HRESULT hr = SHGetUIObjectFromFullPIDL(pidlFolder, NULL, IID_PPV_ARG(IShellLink, &psl));
        
        if (SUCCEEDED(hr))
        {
            hr = psl->GetIDList(ppidl);
            psl->Release();
        }
        
        // It's not a folder shortcut so get the PIDL normally.
        if (FAILED(hr))
            hr = SHILClone(pidlFolder, ppidl);
        
        return hr;
    }// Get the target folder for a folder PIDL. This deals with cases where a folder
    // is an alias to a real folder, folder shortcuts, the My Documents folder, etc.
    STDAPI SHGetTargetFolderPath(LPCITEMIDLIST pidlFolder, LPWSTR pszPath, UINT cchPath)
    {
        LPITEMIDLIST pidlTarget;

        *pszPath = 0;    HRESULT hr = SHGetTargetFolderIDList(pidlFolder, &pidlTarget);
        
        if (SUCCEEDED(hr))
        {
            SHGetPathFromIDListW(pidlTarget, pszPath);   // Make sure it is a path
            CoTaskMemFree(pidlTarget);
        }
        
        return *pszPath ? S_OK : E_FAIL;
    }
      

  3.   

    CString sFolderPath;
        BROWSEINFO bi;
        char Buffer[MAX_PATH];
        //初始化入口参数bi开始
        bi.hwndOwner = NULL;
        bi.pidlRoot = NULL;
        bi.pszDisplayName = Buffer;//此参数如为NULL则不能显示对话框
        bi.lpszTitle = "修改接收路径";
        bi.ulFlags = 0;
        bi.lpfn = NULL;
    //  bi.iImage=IDI_ICON2;
        LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
        if(pIDList)
    {
           SHGetPathFromIDList(pIDList, Buffer);
          //取得文件夹路径到Buffer里
           sFolderPath = Buffer;        //将路径保存在一个CString对象
           m_ListBox.AddString(sFolderPath);
    }
        LPMALLOC lpMalloc;
        if(FAILED(SHGetMalloc(&lpMalloc))) return;
         //释放内存
        lpMalloc->Free(pIDList);
        lpMalloc->Release();