当选择文件数量比较多的时候,发现CFileDialog返回文件名并不完整,翻阅MSDN发现文件名长度是有限制的。解决思路:
CFileDialog dlgOpen(TRUE, _T("*.txt"), NULL, OFN_HIDEREADONLY | OFN_READONLY | OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST, _T("*.txt|*.txt"), this);
const int MIN_FILE_NUMBER = 50; //这里假设最多允许选择50个文件
dlgOpen.m_ofn.lpstrFile = new TCHAR[_MAX_PATH * MIN_FILE_NUMBER];
memset(dlgOpen.m_ofn.lpstrFile, 0, _MAX_PATH * MIN_FILE_NUMBER);
dlgOpen.m_ofn.nMaxFile = _MAX_PATH * MIN_FILE_NUMBER;
dlgOpen.m_ofn.lpstrTitle = _T("请选择txt文件");
dlgOpen.m_ofn.lpstrInitialDir = _T("./");
INT_PTR result = dlgOpen.DoModal();
if (result == IDOK) {
std::vector<CString> vecFiles;
POSITION pos = dlgOpen.GetStartPosition();
while (pos)
{
vecFiles.push_back(dlgOpen.GetNextPathName(pos));
}
//...
}