MFC中请问能不能用一个CFileDialog同时打开多个文件?那样是怎么设置的呢?
就像图片中显示的那样,同时选中,并且读入,怎样才能产生这样的效果呢?既然能够读进去,那怎样对这些文件同时进行读取呢?我怎么对单个文件进行操作呢?...
就像图片中显示的那样,同时选中,并且读入,怎样才能产生这样的效果呢?
既然能够读进去,那怎样对这些文件同时进行读取呢?我怎么对单个文件进行操作呢? 展开
既然能够读进去,那怎样对这些文件同时进行读取呢?我怎么对单个文件进行操作呢? 展开
2个回答
展开全部
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);
}
// 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://hi.baidu.com/whdzx/blog/item/47ae5c367edd48dda2cc2b7e.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询