SHBrowseForFolder

Select the current or default folder using SHBrowseForFolder

The function "SHBrowseForFolder" does not allow to specify the current (default) folder directly, so we need to send "BFFM_SETSELECTION" using the callback function. The folder is specified in the BROWSEINFO lparam.

 

 

Sample Sourcecode:

// callback function
INT CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
{
    if (uMsg==BFFM_INITIALIZED) SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
    return 0;
}

// browseforfolder function
// returns the folder or an empty string if no folder was selected
// hwnd = handle to parent window
// title = text in dialog
// folder = selected (default) folder

CString BrowseForFolder(HWND hwnd, CString title, CString folder)
{
    CString ret;

    BROWSEINFO br;
    ZeroMemory(&br, sizeof(BROWSEINFO));
    br.lpfn = BrowseCallbackProc;
    br.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
    br.hwndOwner = hwnd;
    br.lpszTitle = title;
    br.lParam = (LPARAM)folder.GetString();

    LPITEMIDLIST pidl = NULL;
    if ((pidl = SHBrowseForFolder(&br)) != NULL)
    {
        wchar_t buffer[MAX_PATH];
        if (SHGetPathFromIDList(pidl, buffer)) ret = buffer;
    }

    return ret;
}

// sample function call
CString folder = BrowseForFolder (this->m_hWnd, L"Select Folder", L"C:\\");

Disclaimer: The information on this page is provided "as is" without warranty of any kind. Further, Arclab Software does not warrant, guarantee, or make any representations regarding the use, or the results of use, in terms of correctness, accuracy, reliability, currentness, or otherwise. See: License Agreement