在MFC对话框中选择文件夹收藏

void CBianLiDlg::OnSelectFolder()
{
CString str;
BROWSEINFO bi;
char name[MAX_PATH];
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner = GetSafeHwnd();
bi.pszDisplayName = name;
bi.lpszTitle = "Select folder";
//bi.ulFlags = BIF_USENEWUI;
bi.ulFlags = BIF_RETURNFSANCESTORS;
LPITEMIDLIST idl = SHBrowseForFolder(&bi);
if(idl == NULL)
return;
SHGetPathFromIDList(idl, str.GetBuffer(MAX_PATH));
str.ReleaseBuffer();
m_root = str;//为对话框中与一编辑框对应的CString型变量,保存并显示选中的路径。
if(str.GetAt(str.GetLength()-1)!='/')
m_root+="/";
UpdateData(FALSE); 
}
void CBianLiDlg::FileSearch(CString root)
{ // root 为目录名
CFileFind ff;
CString FilePath;
if (root.Right(1)!="/")
{
root+="/";
}
root+="*.*";
BOOL res=ff.FindFile(root);
while (res)
{
res=ff.FindNextFile();
FilePath=ff.GetFilePath();
if (ff.IsDirectory() && !ff.IsDots())// 找到的是文件夹
{
FileSearch(FilePath);// 递归
}
else if (!ff.IsDirectory() && !ff.IsDots())// 找到的是文件
{
m_ff+=FilePath;
m_ff+=" ";
}
}
}
多文件选择
CFileDialog Dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT);
if(Dlg.DoModal()==IDOK)
{
POSITION pos = Dlg.GetStartPosition();
while(pos)
{
CString szFileName = Dlg.GetNextPathName(pos);
AfxMessageBox(szFileName);
}
}

本文介绍如何在MFC应用中实现文件夹选择和遍历功能。通过`CFileFind`类进行文件及子文件夹的查找,利用`FileSearch`函数进行递归搜索,同时展示如何处理多文件选择对话框,获取并显示选定文件的路径。

744

被折叠的 条评论
为什么被折叠?



