1、批量选择文件
- // 批量添加文件列表
- void AddFile()
- {
- CString szFileName;
- CString szCount;
- POSITION pos;
- int i;
- int nCount=0; //文件数量
- //输入文件
- CFileDialog InFile( TRUE,NULL,NULL,OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT,_T("某种格式|*.fmt|所有文件(*.*)|*.*||") );
- InFile.m_ofn.nMaxFile = 10000; //文件上限
- InFile.m_ofn.lpstrFile = (LPWSTR)malloc(InFile.m_ofn.nMaxFile*sizeof(TCHAR));
- memset(InFile.m_ofn.lpstrFile,0,InFile.m_ofn.nMaxFile);
- //按了OK按钮,开始获取文件列表
- if(InFile.DoModal() == IDOK)
- {
- pos=InFile.GetStartPosition();
- while(pos)
- {
- szFileName=InFile.GetNextPathName(pos);
- //这里szFileName就是文件路径
- //做点什么事情,比如添加到列表框
- }
- }
- //释放空间
- free(InFile.m_ofn.lpstrFile);
- }
2、选择文件夹
- void SelectPath()
- {
- //选择输出路径
- TCHAR szDir[MAX_PATH];
- BROWSEINFO bi;
- ITEMIDLIST *pidl;
- bi.hwndOwner = this->m_hWnd;
- bi.pidlRoot = NULL;
- bi.pszDisplayName = szDir;//这个是输出缓冲区
- bi.lpszTitle = _T("选择输出文件夹:"); //标题
- bi.ulFlags = BIF_NEWDIALOGSTYLE;//使用新的界面,在win7中效果较好//BIF_RETURNONLYFSDIRS;
- bi.lpfn = NULL;
- bi.lParam = 0;
- bi.iImage = 0;
- pidl = SHBrowseForFolder(&bi);//弹出对话框
- if (pidl == NULL)//点了取消,或者选择了无效的文件夹则返回NULL
- return;
- if(SHGetPathFromIDList(pidl, szDir))
- szDir = szDir;//szDir就是要获取的文件夹
- //得到之后做点什么
- }