views:

566

answers:

2

In MFC, is there an Open Folder Dialog? That is, rather than choosing a filename, it chooses a folder name? Ideally, I'd like it to be the way Visual Studio does it when navigating for a "Project Location" (when creating a new project), which looks very much like a normal file dialog. But I could make do with one of the vertical tree sort of interfaces if the former doesn't exist.

A: 

Exact same question here. However, this one is for .NET. I guess it shouldn't be difficult to transpose the answers to MFC.

Serge - appTranslator
+3  A: 

This code will get you a open folder dialog (this was taken from somewhere on the web but I don't really know where).

CString szSelectedFolder = "";

// This is the recommended way to select a directory
// in Win95 and NT4.
BROWSEINFO bi;
memset((LPVOID)&bi, 0, sizeof(bi));
TCHAR szDisplayName[_MAX_PATH];
szDisplayName[0] = '\0';
bi.hwndOwner = GetSafeHwnd();
bi.pidlRoot = NULL;
bi.pszDisplayName = szDisplayName;
bi.lpszTitle = "Select a folder";
bi.ulFlags = BIF_RETURNONLYFSDIRS;
// Set the callback function
bi.lpfn = BrowseCallbackProc;

LPITEMIDLIST pIIL = ::SHBrowseForFolder(&bi);
TCHAR szReturnedDir[_MAX_PATH];

BOOL bRet = ::SHGetPathFromIDList(pIIL, (char*)&szReturnedDir);
if (bRet)
{
    if (szReturnedDir != _T(""))
    {
        szSelectedFolder = szReturnedDir;
    }

    LPMALLOC pMalloc;
    HRESULT HR = SHGetMalloc(&pMalloc);
    pMalloc->Free(pIIL);
    pMalloc->Release();
}

you'll also have to implement this callback function:

TCHAR szInitialDir[_MAX_PATH];

// Set the initial path of the folder browser
int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
    // Look for BFFM_INITIALIZED
    if (uMsg == BFFM_INITIALIZED)
    {
        SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)_T(szInitialDir));
    }
    return 0;
}
djeidot
Instead of the memset, I prefer BROWSEINFO bi = {0};
Mark Ransom
It's not quite what I'm after, but it's what I'll have to use - doesn't look like an alternative exists without too much work.
Smashery