加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

MFC文件夹打开,文件打开,文件夹下文件遍历(转载)

(2013-01-21 16:48:22)
标签:

杂谈

分类: C和Cplusplus

http://blog.csdn.net/yaoohfox/article/details/4132002

http://www.cppblog.com/finehai/archive/2009/08/27/94578.html

 

MFC编程中经常会需要用到选择目录和选择文件的界面,以下总结一下本人常用的这两种对话框的生成方法:

选择目录对话框

http://www.cppblog.com/Images/OutliningIndicators/None.gif//选择目录按钮
http://www.cppblog.com/Images/OutliningIndicators/None.gif
void CDcPackerDlg::OnBnClickedDecgen()
http://www.cppblog.com/Images/dot.gif
{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
char szPath[MAX_PATH]; //存放选择的目录路径
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
CString str;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif ZeroMemory(szPath,
sizeof(szPath));
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif BROWSEINFO bi;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif bi.hwndOwner
= m_hWnd;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif bi.pidlRoot
= NULL;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif bi.pszDisplayName
= szPath;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif bi.lpszTitle
= "请选择需要打包的目录:";
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif bi.ulFlags
= 0;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif bi.lpfn
= NULL;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif bi.lParam
= 0;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif bi.iImage
= 0;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
//弹出选择目录对话框
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
LPITEMIDLIST lp = SHBrowseForFolder(&bi);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
if(lp && SHGetPathFromIDList(lp, szPath))
http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif
http://www.cppblog.com/Images/dot.gif{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif str.Format(
"选择的目录为 %s", szPath);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif AfxMessageBox(str);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif http://www.cppblog.com/Images/dot.gif
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }

http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
else
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif AfxMessageBox(
"无效的目录,请重新选择");
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif }


void CBianLiDlg::OnSelectFolder()
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif
...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif CString str;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif BROWSEINFO bi;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
char name[MAX_PATH];
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif ZeroMemory(
&bi,sizeof(BROWSEINFO));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif bi.hwndOwner
= GetSafeHwnd();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif bi.pszDisplayName
= name;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif bi.lpszTitle
= "Select folder";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
//bi.ulFlags = BIF_USENEWUI;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
bi.ulFlags = BIF_RETURNFSANCESTORS;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif LPITEMIDLIST idl
= SHBrowseForFolder(&bi);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
if(idl == NULL)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
return;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif SHGetPathFromIDList(idl, str.GetBuffer(MAX_PATH));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif str.ReleaseBuffer();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif m_root
= str;//为对话框中与一编辑框对应的CString型变量,保存并显示选中的路径。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
if(str.GetAt(str.GetLength()-1)!='/')
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif m_root
+="/";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif UpdateData(FALSE);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif }

文件遍历

void CBianLiDlg::FileSearch(CString root)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif
...{ // root 为目录名
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif CFileFind ff;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif CString FilePath;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
if (root.Right(1)!="/")
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif
...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif root
+="/";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif root
+="*.*";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif BOOL res
=ff.FindFile(root);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
while (res)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif
...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif res
=ff.FindNextFile();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif FilePath
=ff.GetFilePath();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
if (ff.IsDirectory() && !ff.IsDots())// 找到的是文件夹
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif
...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif FileSearch(FilePath);
// 递归
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif
}

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
else if (!ff.IsDirectory() && !ff.IsDots())// 找到的是文件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif
...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif m_ff
+=FilePath;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif m_ff
+=" ";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif }

多文件选择

CFileDialog Dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
if(Dlg.DoModal()==IDOK)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif
...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif POSITION pos
= Dlg.GetStartPosition();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
while(pos)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif
...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif CString szFileName
= Dlg.GetNextPathName(pos);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif AfxMessageBox(szFileName);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif }

http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif }


选择文件对话框

http://www.cppblog.com/Images/OutliningIndicators/None.gifCString CDcPackerDlg::BootOpenDialog() //返回选择的文件名称
MFC文件夹打开,文件打开,文件夹下文件遍历(转载)MFC文件夹打开,文件打开,文件夹下文件遍历(转载)
http://www.cppblog.com/Images/dot.gif{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif CString strFile
= _T("");
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T(
"Describe Files (*.cfg)|*.cfg|All Files (*.*)|*.*||"), NULL);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
if (dlgFile.DoModal())
http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif
http://www.cppblog.com/Images/dot.gif{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif strFile
= dlgFile.GetPathName();
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }

http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
return strFile;
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif }

http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gif
//加载文件按钮
http://www.cppblog.com/Images/OutliningIndicators/None.gif
void CDcPackerDlg::OnBnClickedSelectdec()
MFC文件夹打开,文件打开,文件夹下文件遍历(转载)MFC文件夹打开,文件打开,文件夹下文件遍历(转载)
http://www.cppblog.com/Images/dot.gif{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
// TODO: Add your control notification handler code here
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
m_strDescPath = ""; //类的成员变量
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
//"打开文件"对话框,选择文件,返回其路径
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
m_strDescPath = BootOpenDialog();
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif http://www.cppblog.com/Images/dot.gif
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有