原文链接:http://blog.sina.com.cn/s/blog_4a08244901018g5z.html (原文也是转载的)
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);
}
}
选择文件对话框

























