// Open dialog pItemIDList = SHBrowseForFolder(&browseInfo);
// Get path SHGetPathFromIDList(pItemIDList, szPath);
// If OK button was pressed and a valid path was selected if (szPath[0]) { // Do something with szPath ... m_strRootPath=szPath; strFind=m_strRootPath+"\\*.*"; UpdateData(FALSE);
BROWSEINFO bi;
LPITEMIDLIST pidl;
::ZeroMemory(m_directory,sizeof(m_directory));
::ZeroMemory(&bi, sizeof(bi));
// Gets the Shell's default allocator
if (::SHGetMalloc(&pMalloc) == NOERROR)
{
// Get help on BROWSEINFO struct - it's got all the bit settings.
bi.hwndOwner=NULL;
bi.pidlRoot=NULL;//为NULL,则已桌面为根,
bi.lpszTitle=_T("请选择文件夹");//
bi.ulFlags=BIF_RETURNONLYFSDIRS;
bi.lpfn=NULL;
bi.lParam=NULL;
bi.pszDisplayName = m_directory;
// This next call issues the dialog box.
if ((pidl = ::SHBrowseForFolder(&bi)) != NULL)
{
if (!::SHGetPathFromIDList(pidl, m_directory))
pMalloc->Free(pidl);
CString strDir = m_directory;
SetDlgItemText(IDC_EDIT1,strDir );
}
pMalloc->Release();
} //
否则是CFileDialog的调用
答】
#include <windows.h>
#include <string.h>
//This is needed for virtually
//everything in BrowseFolder.
#include <shlobj.h>
//BROWSE FOLDER - Opens a browse folder dialog.
void BrowseFolder( void )
{
TCHAR path[MAX_PATH];
BROWSEINFO bi = { 0 };
bi.lpszTitle = ("All Folders Automatically Recursed.");
LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
if ( pidl != 0 )
{
// get the name of the folder and put it in path
SHGetPathFromIDList ( pidl, path );
//Set the current directory to path
SetCurrentDirectory ( path );
//Begin the search
SearchFolder( path );
// free memory used
IMalloc * imalloc = 0;
if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
{
imalloc->Free ( pidl );
imalloc->Release ( );
}
}
}//BROWSE FOLDER
LPITEMIDLIST pItemIDList;
TCHAR szDisplayName[256] = "";
TCHAR szPath[MAX_PATH];
// Initialize browse info structure
browseInfo.hwndOwner = m_hWnd;
browseInfo.pidlRoot = NULL;
browseInfo.pszDisplayName = szDisplayName;
browseInfo.lpszTitle = "请选择目录:";
browseInfo.lParam = 0;
browseInfo.lpfn = NULL;
browseInfo.ulFlags = 0;//BIF_BROWSEINCLUDEFILES | BIF_EDITBOX | BIF_DONTGOBELOWDOMAIN ;
browseInfo.iImage = 0;
// Open dialog
pItemIDList = SHBrowseForFolder(&browseInfo);
// Get path
SHGetPathFromIDList(pItemIDList, szPath);
// If OK button was pressed and a valid path was selected
if (szPath[0])
{
// Do something with szPath ...
m_strRootPath=szPath;
strFind=m_strRootPath+"\\*.*";
UpdateData(FALSE);