关于使用getopenfilename多选多个文件是可以的。

以下是多选文件的代码

 1 bool GetNeedOpenFilePath(vector<tstring>& vectFiles, HWND hwnd )
 2 {
 3     OPENFILENAME ofn;
 4     TCHAR szOpenFileNames[80*MAX_PATH] = {0};
 5     TCHAR szPath[MAX_PATH];
 6     TCHAR szFileName[80*MAX_PATH];
 7 
 8     int nLen = 0;
 9     TCHAR* p = NULL;
10     ZeroMemory(&ofn, sizeof(ofn));
11 
12     ofn.lStructSize = sizeof(ofn);
13     ofn.hwndOwner = hwnd;
14     //ofn.hInstance = (HMODULE)GetCurrentProcess();
15     ofn.lpstrFile = szOpenFileNames;
16     ofn.nMaxFile = sizeof(szOpenFileNames);
17     ofn.lpstrFile[0] = _T('\0');
18     ofn.lpstrFilter = _T("All Files(*.*)\0*.*\0\0");
19     ofn.nFilterIndex = 1;  
20     ofn.lpstrFileTitle = NULL;  
21     ofn.nMaxFileTitle = 0;
22     ofn.lpstrTitle = I18NSTR(L"#IDS_FILESYSTEM_OPEN_FILE_DIR");
23     ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST| OFN_ALLOWMULTISELECT| OFN_HIDEREADONLY /*| OFN_ENABLEHOOK *//*| OFN_ENABLEINCLUDENOTIFY*/| OFN_EXPLORER;
24     //ofn.lpfnHook = OFNHookProc;
25     if (!::GetOpenFileName(&ofn))  //如果打开文件失败,则不操作
26     {
27         return false;
28     }
29 
30     //把第一个文件名前的复制到szPath,即:  
31     //如果只选了一个文件,就复制到最后一个'/'  
32     //如果选了多个文件,就复制到第一个NULL字符  
33     lstrcpyn(szPath,szOpenFileNames,ofn.nFileOffset);
34 
35     //当只选了一个文件时,下面这个NULL字符是必需的.  
36     //这里不区别对待选了一个和多个文件的情况
37 
38     szPath[ofn.nFileOffset] = '\0';
39     nLen = lstrlen(szPath);
40     if (szPath[nLen-1] != '\\')  //如果选了多个文件,则必须加上'//'  
41     {
42         lstrcat(szPath,_T("\\"));
43     }
44 
45     p = szOpenFileNames + ofn.nFileOffset;  //把指针移到第一个文件  
46     ZeroMemory(szFileName, sizeof(szFileName));
47 
48     while(*p)
49     {
50         //lstrcat(szFileName, szPath);     //给文件名加上路径
51         //lstrcat(szFileName, p);             //加上文件名
52         tstring tstrFile = _UTILITY::TCHARToUnicode(p);
53         tstring tstrFilePath = szPath;
54         tstrFilePath += tstrFile;
55         vectFiles.push_back(tstrFilePath);
56         //lstrcat(szFileName, _T("\n"));   //换行
57         p += lstrlen(p) + 1;             //移至下一个文件
58     }
59 
60     return true;
61 }

但是关于使用getopenfilename()函数获取多选文件夹的路径问题,需要自定义一些模板资源。具体实现方式还需研究下。

 

使用getsavefilename()函数可以获取另存为路径:

 1 bool GetSaveAsFilePath(tstring tstrFilePath, HWND hwnd)
 2 {
 3     OPENFILENAME ofn;
 4     TCHAR szOpenFileNames[80*MAX_PATH] = {0};
 5     TCHAR szPath[MAX_PATH];
 6     TCHAR szFileName[80*MAX_PATH];
 7 
 8     int nLen = 0;
 9     TCHAR* p = NULL;
10     ZeroMemory(&ofn, sizeof(ofn));
11 
12     ofn.lStructSize = sizeof(ofn);
13     ofn.hwndOwner = NULL;
14     //ofn.hInstance = (HMODULE)GetCurrentProcess();
15     ofn.lpstrFile = szOpenFileNames;
16     ofn.nMaxFile = sizeof(szOpenFileNames);
17     ofn.lpstrFile[0] = _T('\0');
18     ofn.lpstrFilter = _T("All Files(*.*)\0*.*\0\0");
19     ofn.nFilterIndex = 1;  
20     ofn.lpstrFileTitle = NULL;  
21     ofn.nMaxFileTitle = 0;
22     ofn.lpstrTitle = I18NSTR(L"#IDS_FILESYSTEM_SAVE_AS");
23     ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_EXPLORER;
24     ofn.FlagsEx = OFN_EX_NOPLACESBAR;
25     //ofn.lpfnHook = OFNHookProc;
26     if (!::GetSaveFileName(&ofn))  //如果打开文件失败,则不操作
27     {
28         return false;
29     }
30 
31     tstrFilePath = ofn.lpstrFile;
32     return true;
33 }

 

posted on 2015-12-02 09:49  言止予思  阅读(2538)  评论(0编辑  收藏  举报