int CALLBACK BrowseCallbackProc(HWND hwnd,
UINT uMsg,
LPARAM lParam,
LPARAM lpData
)
{
char szPath[MAX_PATH];
switch(uMsg)
{
case BFFM_INITIALIZED:
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
break;
case BFFM_SELCHANGED:
if (SHGetPathFromIDList((LPITEMIDLIST) lParam ,szPath))
{
SendMessage(hwnd, BFFM_SETSTATUSTEXT,0,(LPARAM)szPath);
}
break;
}
// if((uMsg & BFFM_INITIALIZED) == BFFM_INITIALIZED)
// {
//SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
// }
return 0;
}
void CSystemParam::OnBnClickedBtnPathBrowse()
{
//ITEMIDLIST *ppidl;
//SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP ,&ppidl);
//if ( ppidl == NULL)
//{
// AfxMessageBox(_T("启动路径浏览失败") ) ;
// return;
//}
BROWSEINFO bi;
memset(&bi,0,sizeof(BROWSEINFO));
bi.lpszTitle = _T("请选择一个数据文件保存文件");
bi.lpfn = BrowseCallbackProc;
bi.lParam = (LPARAM)(LPCTSTR)m_sFileSavePath;
bi.ulFlags = BIF_RETURNONLYFSDIRS /*| BIF_EDITBOX*/ | BIF_STATUSTEXT /*| BIF_NEWDIALOGSTYLE*/;
LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
if ( pidl != 0 )
{
// get the name of the folder
TCHAR path[MAX_PATH];
if ( SHGetPathFromIDList ( pidl, path ) )
{
m_sFileSavePath.Format(_T("%s"),path);
if(m_sFileSavePath.GetAt(m_sFileSavePath.GetLength()-1)!=_T('//'))
m_sFileSavePath.AppendChar(_T('//'));
UpdateData(FALSE);
}
// free memory used
IMalloc * imalloc = 0;
if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
{
imalloc->Free ( pidl );
imalloc->Release ( );
}
}
}
参考资料:
The ''Browse For Folder'' dialog allows the user to select a folder from all available local drives and network resources.
The following code snippet demonstrate how to display this dialog-box. The BrowseFolders function accept 3 parameters:
hwnd - The handle of the parent window
lpszFolder - Address of a buffer to receive the selected folder. Before calling this function, you should fill this buffer with the folder that you want to be selected when the dialog-box is loaded at first.
lpszTitle - The text that should be displayed as a title of the ''Browse Folders'' dialog-box
Return back to C/C++ code index
#include
#include
int CALLBACK BrowseForFolderCallback(HWND hwnd,UINT uMsg,LPARAM lp, LPARAM pData)
{
char szPath[MAX_PATH];
switch(uMsg)
{
case BFFM_INITIALIZED:
SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
break;
case BFFM_SELCHANGED:
if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szPath))
{
SendMessage(hwnd, BFFM_SETSTATUSTEXT,0,(LPARAM)szPath);
}
break;
}
return 0;
}
BOOL BrowseFolders(HWND hwnd, LPSTR lpszFolder, LPSTR lpszTitle)
{
BROWSEINFO bi;
char szPath[MAX_PATH + 1];
LPITEMIDLIST pidl;
BOOL bResult = FALSE;
LPMALLOC pMalloc;
if (SUCCEEDED(SHGetMalloc(&pMalloc)))
{
bi.hwndOwner = hwnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = NULL;
bi.lpszTitle = lpszTitle;
bi.ulFlags = BIF_STATUSTEXT; //BIF_EDITBOX
bi.lpfn = BrowseForFolderCallback;
bi.lParam = (LPARAM)lpszFolder;
pidl = SHBrowseForFolder(&bi);
if (pidl)
{
if (SHGetPathFromIDList(pidl,szPath))
{
bResult = TRUE;
strcpy(lpszFolder, szPath);
}
pMalloc->Free(pidl);
pMalloc->Release();
}
}
return bResult;
}
文章出处:http://www.diybl.com/course/3_program/c++/cppsl/2008520/117219.html