转:https://www.cnblogs.com/MakeView660/p/6400083.html
实际工作开发中,由于各种应用,我们需要调用系统的打开文件对话框或者打开文件夹对话框,或两者兼有。特总结了常用的实现方法,仅供开发参考。
1、 打开文件对话框
常用的方法是使用系统的CFileDialog。
在此介绍另外一种方法就是使用OPENFILENAME结构和GetOpenFileName()函数,可以实现单选文件或者多选文件,代码如下:
注意:包含头文件 #include "CommDlg.h"
TCHAR szBuffer[MAX_PATH] = {0}; OPENFILENAME ofn = {0}; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = m_hWnd; ofn.lpstrFilter = _T("exe文件(*.exe) *.exe 所有文件(*.*) *.* "); // 文件过滤 ofn.lpstrInitialDir = _T("D:\Program Files"); // 默认文件目录 ofn.lpstrFile = szBuffer; ofn.nMaxFile = sizeof(szBuffer)/sizeof(*szBuffer); ofn.nFilterIndex = 0; ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER ; BOOL bSel = GetOpenFileName(&ofn); if (bSel) { ::MessageBox(NULL, szBuffer, TEXT("SignalSelect"), MB_OK); }
上面的代码演示了打开单个文件的情况,下面的代码则演示了如何得到多选的情况下选择的文件名。
OPENFILENAME ofn; TCHAR szOpenFileNames[80 * MAX_PATH] = {0}; TCHAR szPath[MAX_PATH] = {0}; TCHAR szFileName[80 * MAX_PATH] = {0}; TCHAR* p = NULL; int nLen = 0; ZeroMemory( &ofn, sizeof(ofn) ); ofn.Flags = OFN_EXPLORER | OFN_ALLOWMULTISELECT; ofn.lStructSize = sizeof(ofn); ofn.lpstrFile = szOpenFileNames; ofn.nMaxFile = sizeof(szOpenFileNames); ofn.lpstrFile[0] = ' '; ofn.lpstrFilter = TEXT("All Files(*.*) *.* "); if( GetOpenFileName( &ofn ) ) { lstrcpyn(szPath, szOpenFileNames, ofn.nFileOffset ); szPath[ ofn.nFileOffset ] = ' '; nLen = lstrlen(szPath); if( szPath[nLen-1] != '\' ) //如果选了多个文件,则必须加上'//' { lstrcat(szPath, TEXT("\")); } p = szOpenFileNames + ofn.nFileOffset; //把指针移到第一个文件 ZeroMemory(szFileName, sizeof(szFileName)); while( *p ) { lstrcat(szFileName, szPath); //给文件名加上路径 lstrcat(szFileName, p); //加上文件名 lstrcat(szFileName, TEXT(" ")); p += lstrlen(p) +1; //移至下一个文件 } ::MessageBox(NULL, szFileName, TEXT("MultiSelect"), MB_OK); }
2、打开文件夹对话框
打开文件夹对话框使用BROWSEINFO结构和SHBrowseForFolder ()函数。
注意:包含头文件 #include "Shlobj.h"
// 选择单个目录
TCHAR szBuffer[MAX_PATH] = {0}; BROWSEINFO bi; ZeroMemory(&bi,sizeof(BROWSEINFO)); bi.hwndOwner = NULL; bi.pszDisplayName = szBuffer; bi.lpszTitle = _T("从下面选文件夹目录:"); bi.ulFlags = BIF_RETURNFSANCESTORS; LPITEMIDLIST idl = SHBrowseForFolder(&bi); if (NULL == idl) { return; } SHGetPathFromIDList(idl,szBuffer); ::MessageBox(NULL, szBuffer, TEXT("FolderSelect"), MB_OK);
// 选择单个目录,带新建文件夹
TCHAR szPath[_MAX_PATH] = {0}; BROWSEINFO bi; bi.hwndOwner = NULL; bi.pidlRoot = NULL; bi.lpszTitle = _T("选择工程目录"); bi.pszDisplayName = szPath; bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE; //标记新建按钮 bi.lpfn = NULL; bi.lParam = NULL; LPITEMIDLIST pItemIDList = SHBrowseForFolder(&bi); if(pItemIDList) { SHGetPathFromIDList(pItemIDList, szPath); ::MessageBox(NULL, szPath, TEXT("FolderSelect"), MB_OK); } IMalloc * imalloc = 0; if ( SUCCEEDED(SHGetMalloc( &imalloc))) { imalloc->Free(pItemIDList); imalloc->Release(); }
// 选择文件或目录
TCHAR szBuffer[MAX_PATH] = {0}; BROWSEINFO bi; ZeroMemory(&bi,sizeof(BROWSEINFO)); bi.hwndOwner = NULL; bi.pszDisplayName = szBuffer; bi.lpszTitle = _T("从下面选择文件或文件夹:"); bi.ulFlags = BIF_BROWSEINCLUDEFILES; LPITEMIDLIST idl = SHBrowseForFolder(&bi); if (NULL == idl) { return; } SHGetPathFromIDList(idl,szBuffer); ::MessageBox(NULL, szBuffer, TEXT("Folder/File Select"), MB_OK);
好了,几种简单的实现就介绍到这里,如果有更好的方法请扩展分享,分享的快乐才是双倍的快乐!