16,472
社区成员
发帖
与我相关
我的任务
分享
// 修改m_ofn.lpstrFile指向的buf大小,默认的大小太小,此处根据FILE_MAX_COUNT来设定
int nBufLen = FILE_MAX_COUNT*(MAX_PATH+1) + 1;
TCHAR* pBuf = new TCHAR[nBufLen];
memset( pBuf, 0, nBufLen*sizeof(TCHAR) );
OPENFILENAME ofn;
memset( &ofn, 0 ,sizeof(ofn) );
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = m_hParentWnd;
ofn.lpstrFile = pBuf;
ofn.nMaxFile = nBufLen;
ofn.lpstrFilter = _T("All Files(*.*)\0*.*\0\0");
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_EXPLORER|OFN_ALLOWMULTISELECT|OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_NODEREFERENCELINKS;
if ( ::GetOpenFileName( &ofn ) )
{
int nNewFileItemCount = 0;
pos = (POSITION)ofn.lpstrFile;
while ( pos != NULL )
{
CString strPathName = GetNextPathName( pos, ofn ); // 依次找到选择的多个文件名
}
}
else
{
// 存放用户选择文件路径信息的buf大小有限,当选择的文件过多时,文件信息长度超过定长buf时,DoModal函数
// 会返回IDCANCEL,CommDlgExtendedError()将返回FNERR_BUFFERTOOSMALL错误,2012/06/05
DWORD dwErr = CommDlgExtendedError();
if ( 12291 == dwErr ) // 12291 - FNERR_BUFFERTOOSMALL
{
str.Format( STRING_SELED_FILE_EXCEED_COUNT_LIMIT, TRANSFERRED_FILE_ITEM_MAX_COUNT );
ErrorMsg( str, m_hParentWnd );
}
}
delete []pBuf;
至于如何得到多个选择的文件名,可以参考CFileDialog中的相关代码实现,将代码抽出来封装成GetNextPathName函数,供上面调用(虽然不用MFC类,但是MFC类的很多代码写的非常好,可以直接抽出来使用,安全性和稳定性有绝对的保证)
// 主要用于文件打开对话框选中多个文件时,解析出多个文件名,从CFileDialog::GetNextPathName
// 函数中剥离出来
CString GetNextPathName(POSITION& pos, OPENFILENAME& ofn)
{
BOOL bExplorer = ofn.Flags & OFN_EXPLORER;
TCHAR chDelimiter;
if (bExplorer)
chDelimiter = '\0';
else
chDelimiter = ' ';
LPTSTR lpsz = (LPTSTR)pos;
if (lpsz == ofn.lpstrFile) // first time
{
if ((ofn.Flags & OFN_ALLOWMULTISELECT) == 0)
{
pos = NULL;
return ofn.lpstrFile;
}
// find char pos after first Delimiter
while(*lpsz != chDelimiter && *lpsz != '\0')
lpsz = _tcsinc(lpsz);
lpsz = _tcsinc(lpsz);
// if single selection then return only selection
if (*lpsz == 0)
{
pos = NULL;
return ofn.lpstrFile;
}
}
CString strPath = ofn.lpstrFile;
if (!bExplorer)
{
LPTSTR lpszPath = ofn.lpstrFile;
while(*lpszPath != chDelimiter)
lpszPath = _tcsinc(lpszPath);
strPath = strPath.Left(lpszPath - ofn.lpstrFile);
}
LPTSTR lpszFileName = lpsz;
CString strFileName = lpsz;
// find char pos at next Delimiter
while(*lpsz != chDelimiter && *lpsz != '\0')
lpsz = _tcsinc(lpsz);
if (!bExplorer && *lpsz == '\0')
pos = NULL;
else
{
if (!bExplorer)
strFileName = strFileName.Left(lpsz - lpszFileName);
lpsz = _tcsinc(lpsz);
if (*lpsz == '\0') // if double terminated then done
pos = NULL;
else
pos = (POSITION)lpsz;
}
// only add '\\' if it is needed
if (!strPath.IsEmpty())
{
// check for last back-slash or forward slash (handles DBCS)
LPCTSTR lpsz = _tcsrchr(strPath, '\\');
if (lpsz == NULL)
lpsz = _tcsrchr(strPath, '/');
// if it is also the last character, then we don't need an extra
if (lpsz != NULL &&
(lpsz - (LPCTSTR)strPath) == strPath.GetLength()-1)
{
ASSERT(*lpsz == '\\' || *lpsz == '/');
return strPath + strFileName;
}
}
return strPath + '\\' + strFileName;
}
TCHAR szFile[MAX_PATH] = { 0 };
_tcscpy( szFile, _T("123.txt") ); // 初始化文件名
OPENFILENAME ofn;
memset( &ofn, 0 ,sizeof(ofn) );
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = m_hParentWnd;
ofn.lpstrFilter = _T("记录文件(*.txt)\0*.txt\0默认格式(*.*)\0*.*\0\0");
ofn.lpstrFile= szFile;
ofn.nMaxFile = sizeof(szFile)/sizeof(TCHAR);
ofn.lpstrDefExt = _T("txt");
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST|OFN_EXPLORER|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT;
if( ::GetSaveFileName( &ofn ) )
{
strSaveFilePathName = ofn.lpstrFile; // 得到用户输入的文件名
}