在 VC++ 6.0, MFC 中我想选择多个文件
CFileDialog opendialog(true); // opens the dialog for open;
opendialog.m_ofn.lpstrTitle="SELECT FILE"; //selects the file title;
opendialog.m_ofn.lpstrFilter="text files (*.txt)\0*.txt\0"; //selects the filter;
if(opendialog.DoModal()==IDOK) //checks wether ok or cancel button is pressed;
{
srcfilename=opendialog.GetPathName(); //gets the path name;
...
}
上面的代码示例一次只允许选择一个文件,但我想选择多个文本文件,例如按住 control 键(ctrl+选择多个文件)。我怎样才能做到这一点?
最佳答案
因此,在 CFileDialog 的构造函数中,您可以将 dwFlags 参数设置为“OFN_ALLOWMULTISELECT”。这是最简单的部分,要实际获取多个文件名,您必须修改 CFileDialog 中的 m_ofn.lpstrFile 成员以指向您已分配的缓冲区。看看这里:
http://msdn.microsoft.com/en-us/library/wh5hz49d(VS.80).aspx
这是一个使用它的示例,希望评论足够:
void CMainFrame::OnFileOpen()
{
char strFilter[] = { "Rule Profile (*.txt)|*.txt*||" };
CFileDialog FileDlg(TRUE, "txt", NULL, OFN_ALLOWMULTISELECT, strFilter);
CString str;
int nMaxFiles = 256;
int nBufferSz = nMaxFiles*256 + 1;
FileDlg.GetOFN().lpstrFile = str.GetBuffer(nBufferSz);
if( FileDlg.DoModal() == IDOK )
{
// The resulting string should contain first the file path:
int pos = str.Find(' ', 0);
if ( pos == -1 );
//error here
CString FilePath = str.Left(pos);
// Each file name is seperated by a space (old style dialog), by a NULL character (explorer dialog)
while ( (pos = str.Find(' ', pos)) != -1 )
{ // Do stuff with strings
}
}
else
return;
}
关于mfc - 在 CFileDialog 中选择多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1107913/