Posted in Win32/MFC on February 17, 2006 |
6 Comments »
Today, there was an article posted on the Code Project website that showed how to use SHBrowseForFolder, and there was a question from a reader in the forum, asking how a default folder can be specified. This is pretty easy to do, and all you need to do is to set the BIF_VALIDATE flag, specify a callback, and in the callback, handle BFFM_INITIALIZED which indicates that the dialog is ready, and then SendMessage a BFFM_SETSELECTION message to the HWND of the dialog. Here’s some commented code that shows how this is done.
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("C:\\Program Files"));
}
return 0;
}
void ShowSHBrowseForFolderDemoDlg()
{
BROWSEINFO bi = {0};
// Make sure BIF_VALIDATE is specified
bi.ulFlags = BIF_USENEWUI|BIF_VALIDATE;
bi.lpszTitle = _T("Choose a folder");
// Set the callback function
bi.lpfn = BrowseCallbackProc;
LPITEMIDLIST pIDL = SHBrowseForFolder(&bi);
if(pIDL)
{
// Your code goes here...
CoTaskMemFree(pIDL);
}
}
Read Full Post »