开发者

SHBrowseForFolder and shortcuts

In my C++ Windows application, I have a function that is supposed to allow the end-user select a folder. I'm using SHBrowseForFolder and it is working fine except that folder shortcuts are not being displayed in the dialog.

Does anyone know if it is possible to configure SHBrowseForFolder so that the end-users will be able to navigate folder shortcuts?

Edit: May 24th, 2010, 1:10 EST:

Okay, I'm going to show the code of what I have so far. I have tried using the suggestion of putting a BFFM_IUNKNOWN callback in my callback procedure, but have been struggling to figure out how to provide a IFolderFilter descendent that works.

1. The code that gets called:

Error CFolderChooserDialog::RunDialog()  
{  
    Error runResult = kError_NotInitialized;

    if (VERIFYN(kLyndsey, m_ReferenceCount > 0)) {  
         runResult = kError_Unexpected;  

         m_AllFoldersFilter = new TAllFoldersFilter();  

         if (VERIFYN(kLyndsey, m_AllFoldersFilter))  
         {  
               char selectedDirectoryBuffer[MAX_PATH];  
               m_DirectoryPath.CopyInto(selectedDirectoryBuffer);  

               BROWSEINFO bi;  
               memset(&bi, 0, sizeof(bi));  

               bi.hwndOwner = MyGetMainHWND(m_CBP);  
               bi.pidlRoot = NULL;  
               bi.pszDisplayName = selectedDirectoryBuffer;  
               bi.lpszTitle = (const char*)m_Description;  
               bi.ulFlags |= BIF_RETURNONLYFSDIRS;  
               bi.ulFlags |= BIF_BROWSEINCLUDEFILES;  

               bi.lpfn = SHBrowseForFolderCallbackProc;  
               bi.lParam = (LPARAM)this;  
               bi.iImage = 0;  

               LPITEMIDLIST resultInfo = SHBrowseForFolder(&bi);  
               if (resultInfo) {  
                   runResult = kError_NoError;  
                   if (SHGetPathFromIDList(resultInfo, selectedDirectoryBuffer)) {  
                        m_DirectoryPath = selectedDirectoryBuffer;  
                   }  
              }  
              else {  
                   runResult = kError_Failed;  
              }  
               delete m_AllFoldersFilter;  
               m_AllFoldersFilter = nil;  
               CoTaskMemFree(resultInfo);  
          }  

    }  

    return runResult;  
}  

2. The callback that gets called from SHBrowseForFolder:

int CALLBACK CFolderChooserDialog::SHBrowseForFolderCallbackProc(HWND window, UINT message, LPARAM messageValue, LPARAM clientData)  
{  
    CFolderChooserDialog* thisPtr = (CFolderChooserDialog*)clientData;  

    if (VERIFYN(kLyndsey, thisPtr)) {  
        switch (message) {  
            case BFFM_INITIALIZED: {  
                if (!thisPtr->m_DialogTitle.IsEmpty()) {  
                    ::SetWindowText(window, (const char*) thisPtr->m_DialogTitle);  
                }  

                if (!thisPtr->m_DirectoryPath.IsEmpty()) {  
                    LPCTSTR startDirectory = thisPtr->m_DirectoryPath;  
                    ::SendMessage(window, BFFM_SETSELECTION, TRUE, (LPARAM)startDirectory);  
                }  
                break;  
            }  
            case BFFM_IUNKNOWN:  
            {  
                IUnknown* theInterface = (IUnknown*)messageValue;  
                if (VERIFYN(kLyndsey, theInterface))  
                {  
                    IFolderFilterSite* filter = NULL;  
                    theInterface->QueryInterface(IID_IFolderFilterSite, (void**)&filter);  
                    if (VERIFYN(kLyndsey, filter))  
                    {  
                        filter->SetFilter((IUnknown*)thisPtr->m_AllFoldersFilter);  
                        filter->Release();  
                    }  
                }  
                break;  
            }  
            default:  
                break;  
        }  
    }  
    return 0;  
}  

3. The IFolderFilter that should get called for each item for filtering it in or out of the dialog:

class TAllFoldersFilter : public IFolderFilter  
{  
public:  
    TAllFoldersFilter() { refCount = 0;}  

    HRESULT STDMETHODCALLTYPE QueryInterface(const IID& iid, void**开发者_如何转开发 obj)  
    {  
        if (!obj)  
            return E_INVALIDARG;  
        *obj = NULL;  

        if (iid == IID_IUnknown || iid == IID_IFolderFilter)  
        {  
            *obj = (void*)this;  
            AddRef();  
            return NOERROR;  
        }  
        return E_NOINTERFACE;  
    }  

    ULONG STDMETHODCALLTYPE AddRef()  
    {  
        refCount++;  
        return refCount;  
    }  
    ULONG STDMETHODCALLTYPE Release()  
    {  
        refCount--;  
        return refCount;  
    }  

    HRESULT STDMETHODCALLTYPE GetEnumFlags(IShellFolder* sf, LPCITEMIDLIST pidlFolder, HWND* window, DWORD* flags)  
    {  
        return 0;  
    }  

    HRESULT STDMETHODCALLTYPE ShouldShow(IShellFolder* sf, LPCITEMIDLIST pidlFolder, LPCITEMIDLIST pidlItem)  
    {  
        HRESULT resultCode = S_OK;  

        ULONG attributes = 0UL;  

        if (SUCCEEDED(sf->GetAttributesOf(1, &pidlItem, &attributes)))  
        {  
            if (attributes & SFGAO_FOLDER)  
            {  
                resultCode = S_OK;  // Yes, I see the folders
            }  
            else if (attributes & SFGAO_LINK)  
            {  
                resultCode = S_OK;  // Yes, this shows the folder shortcut links, but I cannot explore them. When I "expand" them (click on the plus-sign-box), nothing happens.
            }  
        }  

        return resultCode;  
    }  
protected:  
    ULONG refCount;  
};  

So, where am I? Well, I can show the folders, I can show folder links, but I'm uncertain about the following:

  1. How do I easily determine if the item I have is a shortcut link to a folder? The code I wrote is definitely not looking at that and is showing any shortcut link.

  2. How do I easily allow the end-user delve into the folder represented by the shortcut link?

  3. Is this code correct and as simple/clean as it can be?

Thanks for all of your help!

Edit: June 1st, 2010: 2:14 EST: The answer was technically provided, so I'll mark that and I'm going to ask another question to help me fix this code.


I guess you could add the BIF_BROWSEINCLUDEFILES style and then filter the items to only display folders and .lnk's to folders (This is what you are after right?)

To filter the items, you need to add a callback func to BROWSEINFO, catch BFFM_IUNKNOWN and query for IFolderFilterSite and set a filter


The better answer is to use IFileOpenDialog with FOS_PICKFOLDERS as the option for Windows Vista and later.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜