VC6.0中CFileDialog怎么选择多个文件?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VC6.0中CFileDialog怎么选择多个文件?相关的知识,希望对你有一定的参考价值。

在VC6.0中使用CFileDialog类时,怎么设定选择多个文件?谢谢!

//同时打开N个文件
void COpenNFileDlg::OnButton1()

CString pathName,fileName,fileTitle;

char* filters = _T("PCM文件(*.pcm)|*.pcm");

//创建一个可以选择多个文件的CFileDialog
CFileDialog fileDlg(true,NULL,"*.pcm",OFN_ALLOWMULTISELECT | OFN_ENABLESIZING | OFN_HIDEREADONLY,filters);

//最多可以打开500个文件
fileDlg.m_ofn.nMaxFile = 500 * MAX_PATH;

char* ch = new TCHAR[fileDlg.m_ofn.nMaxFile];
fileDlg.m_ofn.lpstrFile = ch;

//对内存块清零
ZeroMemory(fileDlg.m_ofn.lpstrFile,sizeof(TCHAR) * fileDlg.m_ofn.nMaxFile);

//显示文件对话框,获得文件名集合
if(fileDlg.DoModal() == IDOK)

//获取第一个文件的位置
POSITION pos_file;
pos_file = fileDlg.GetStartPosition();

//用CString数组存放文件的路径
CArray<CString, CString> ary_filename;
//存放文件的标题
CArray<CString, CString> ary_fileTitle;

//循环读出每个路径并存放在数组中
while(pos_file != NULL)

//将文件路径存放在数组中
pathName = fileDlg.GetNextPathName(pos_file);
ary_filename.Add(pathName);

//获取文件名
//从字符串的后面往前遍历,如果遇到'\'则结束遍历,'\'右边的字符串则为文件名
int length = pathName.GetLength();
for(int i = length -1; i>0;i--)

if('\' == pathName. GetAt(i))
//判断当前字符是否是'\'
fileName = pathName.Right(length - i -1);
break;//跳出循环

//endfor

//获取文件名(不包含后缀)
//采用CString的Left(int count)截取CString中从左往右数的count个字符
//fileName.GetLength()-4中的4表示".dat"四个字符

fileTitle = fileName.Left(fileName.GetLength()-4);
//AfxMessageBox(fileTitle);
ary_fileTitle.Add(fileTitle);//将文件名(不包含后缀)添加到数组中


delete[] ch;
参考技术A 下面代码,可以选择文件夹,但是不能选文件
需要自己查找文件
char* GetPath(HWND hWnd,char* pBuffer)

BROWSEINFO bf;
LPITEMIDLIST lpitem;
memset(&bf,0,sizeof BROWSEINFO);
bf.hwndOwner=hWnd;
bf.lpszTitle= "选择路径";
bf.ulFlags=BIF_RETURNONLYFSDIRS; //属性你可自己选择
lpitem=SHBrowseForFolder(&bf);
if(lpitem==NULL) //如果没有选择路径则返回 0
return 0;
//如果选择了路径则复制路径,返回路径长度
SHGetPathFromIDList(lpitem,pBuffer);
return pBuffer;

----------------------------------------------------
或者

#include "cderr.h " //for definition of FNERR_BUFFERTOOSMALL

CFileDialog dlg( TRUE, NULL, NULL, OFN_ALLOWMULTISELECT, NULL, NULL );
DWORD MAXFILE = 4000
dlg.m_ofn.nMaxFile = MAXFILE;
char* pc = new char[MAXFILE];
dlg.m_ofn.lpstrFile = pc;
dlg.m_ofn.lpstrFile[0] = NULL;

int iReturn = dlg.DoModal();
if(iReturn == IDOK)

int nCount = 0;
POSITION pos = dlg.GetStartPosition();
while (pos != NULL)

dlg.GetNextPathName(pos);
nCount++;

CString str;
str.Format( "Successfully opened %d files\n ", nCount);
AfxMessageBox(str);

else if(iReturn == IDCANCEL)
AfxMessageBox( "Cancel ");

if(CommDlgExtendedError() == FNERR_BUFFERTOOSMALL)
AfxMessageBox( "BUFFERTOOSMALL ");
delete []pc;
--------------------
第二个代码没测试过,不知道好不好使
都是找的本回答被提问者采纳
参考技术B 需要指定OFN_ALLOWMULTISELECT:

#define DLG_FILTER "txt Files (*.txt)|*.txt|| "
#define DLG_EXT "txt "

CFileDialog dlg(TRUE, _T(DLG_EXT), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT, _T(DLG_FILTER));

if (dlg.DoModal() == IDOK)

POSITION pos = dlg.GetStartPosition();

while (pos != 0)

CString s = dlg.GetNextPathName(pos);

以上是关于VC6.0中CFileDialog怎么选择多个文件?的主要内容,如果未能解决你的问题,请参考以下文章

CFileDialog 多文件选择

怎么用CFileDialog 选择文件夹

怎么用CFileDialog 选择文件夹

MFC中CFileDialog使用方法

如何在VC6。0运行C语言程序

win10无法运行vc6.0怎么办