使用CFileDialog打开多个文件并获取文件名(转)
CFileDialog dlg(true, _T("txt"), _T("*.txt")), OFN_ALLOWMULTISELECT);
// Create buffer for file names.
const DWORD numberOfFileNames = 1;
const DWORD fileNameMaxLength = MAX_PATH + 1;
const DWORD bufferSize = (numberOfFileNames * fileNameMaxLength) + 1;
TCHAR* filenamesBuffer = new TCHAR[bufferSize];
// Initialize beginning and end of buffer.
filenamesBuffer[0] = NULL;//必须的
filenamesBuffer[bufferSize-1] = NULL;
// Attach buffer to OPENFILENAME member.
dlg.m_ofn.lpstrFile = filenamesBuffer;
dlg.m_ofn.nMaxFile = bufferSize;
// Create array for file names.
CString fileNameArray[numberOfFileNames];
if(dlg.DoModal() == IDOK)
{
// Retrieve file name(s).
POSITION fileNamesPosition = dlg.GetStartPosition();
int iCtr = 0;
while(fileNamesPosition != NULL)
{
fileNameArray[iCtr] = dlg.GetNextPathName(fileNamesPosition);
iCtr++;
}
}
//Release file names buffer.
delete[] filenamesBuffer;