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

VC6.0中CFileDialog怎么选择多个文件?,第1张

//同时打开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 -1i>0i--)

{

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

}

CFileDialog 打开多个文件2006-12-01 17:28从CFileDialog派生一个类MFileDlg: 头文件://////////派散///////////////////早困////////////////////////////////////////////////

// MFileDlg dialogclass MFileDlg : public CFileDialog

{

DECLARE_DYNAMIC(MFileDlg)public:

MFileDlg(BOOL bOpenFileDialog, // TRUE for FileOpen, FALSE for FileSaveAs

LPCTSTR lpszDefExt = NULL,

LPCTSTR lpszFileName = NULL,

DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,

LPCTSTR lpszFilter = NULL,

CWnd* pParentWnd = NULL)

int DoModal()

virtual ~MFileDlg()

protected:

//{{AFX_MSG(MFileDlg)

// NOTE - the ClassWizard will add and remove member functions here.

//}}AFX_MSG TCHAR* m_pszFile

////////

DECLARE_MESSAGE_MAP()}类实现://////////////////////////////////////////////尘睁氏///////////////////////////////

// MFileDlgIMPLEMENT_DYNAMIC(MFileDlg, CFileDialog)MFileDlg::MFileDlg(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,

DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :

CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)

{

m_pszFile = new TCHAR[2048]//set a 2K buffer to hold selected files

m_pszFile[0] = '\0'//initialize pointer

}

BEGIN_MESSAGE_MAP(MFileDlg, CFileDialog)

//{{AFX_MSG_MAP(MFileDlg)

// NOTE - the ClassWizard will add and remove mapping macros here.

//}}AFX_MSG_MAP

END_MESSAGE_MAP()MFileDlg::~MFileDlg()

{

if (m_pszFile != NULL)

delete [] m_pszFile//cleanup

}int MFileDlg::DoModal()

{

ASSERT_VALID(this)

ASSERT(m_ofn.Flags &OFN_ALLOWMULTISELECT)//make sure multiple file selection is on m_ofn.lpstrFile = m_pszFile//initialize the OPENFILENAME structure

m_ofn.nMaxFile = 2048

return CFileDialog::DoModal()

} 具体使用: MFileDlg dlg( TRUE,_T("TXT"),_T("*.TXT"),

OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,

_T("文本文件 (*.txt)|*.txt所有文件 (*.*)|*.*|"))

if(dlg.DoModal()!=IDOK) return FALSE //取消 *** 作 POSITION pos = dlg.GetStartPosition()

while (pos != NULL)

{

CString strPath = dlg.GetNextPathName(pos)

if (strPath.Find(":\\\\") == 1 &&strPath.GetLength() >4)

{

// this means we have an invalid path that looks like this:

// C:\\cda.dgl

// get rid of extra slash

CString temp

temp = strPath.Left(3)

temp += strPath.Mid(4)

strPath = temp

}

m_listFileNames.AddTail(strPath)//CStringList m_listFileNames

TRACE(_T("GetOpenFileName returned %s Files\r\n"),strPath)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12263634.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存