CFileDialog 打开文件夹文件 保存文件夹文件
格式说明:
explicit CFileDialog(
BOOL bOpenFileDialog, //TRUE 为打开, FALSE 为保存
LPCTSTR lpszDefExt = NULL, // 默认文件扩展名
LPCTSTR lpszFileName = NULL, //文件对话框中 初始的文件名称
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, //设定对话框功能
LPCTSTR lpszFilter = NULL, //文件过滤
CWnd* pParentWnd = NULL,
DWORD dwSize = 0, // The size of theOPENFILENAME structure, 默觉得0 ,表示自己主动确定正确的大小
BOOL bVistaStyle = TRUE
);
參数含义具体说明:
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
Either a File Open or File Save As dialog box is constructed, depending on the value ofbOpenFileDialog.
To enable the user to select multiple files, set the OFN_ALLOWMULTISELECT flag before you callDoModal.
You must supply your own file name buffer to store the returned list of multiple file names. Do this by replacing
m_ofn.lpstrFile with a pointer to a buffer you have allocated, after you construct theCFileDialog, but before you call
DoModal. Additionally, you must set m_ofn.nMaxFile with the number of characters in the buffer pointed to by
m_ofn.lpstrFile. If you set the maximum number of files to be selected ton, the necessary buffer size isn*
(_MAX_PATH + 1) + 1.
实例1 打开文件:
实例2 打开文件:
实例3 打开文件:
实例4 打开文件,并设置对话框标题
CFileDialog nFileDlg(TRUE,L"xml",L"",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,L"XML文件(*.xml)|*.xml||"); nFileDlg.m_pOFN->lpstrTitle=L"打开空白任务"; //文件对话框标题 if(nFileDlg.DoModal()==IDOK) { m_PicFolder=L"Blank"; m_XMLFolder=L"Blank"; CString szXmlFilePath; CString szXmlParentPath; CString nXMLFileName; szXmlFilePath=nFileDlg.GetPathName(); // 绝对路径文件名称 nXMLFileName=nFileDlg.GetFileName(); // 不带路径的文件名称 szXmlParentPath=szXmlFilePath.Left(szXmlFilePath.GetLength()-nXMLFileName.GetLength()-1); //文件所在的父文件夹 }
实例5 保存文件:
以上是使用 CFileDialog打开文件,以下是使用SHBrowseForFolder打开路径:
OnBnClickedButton1() { BROWSEINFO bi; ZeroMemory(&bi,sizeof(BROWSEINFO)); LPMALLOC pMalloc; LPITEMIDLIST pidl = SHBrowseForFolder(&bi); if (pidl==NULL) return; if(pidl != NULL) { TCHAR * path = new TCHAR[MAX_PATH]; SHGetPathFromIDList(pidl,path); // MessageBox(NULL,path,TEXT("Choose"),MB_OK); if(SUCCEEDED(SHGetMalloc(&pMalloc)))//pidl指向的对象用完应该释放,之前忽略了 { pMalloc->Free(pidl); pMalloc->Release(); } m_Path=path; UpdateData(FALSE); delete [] path; } }