CFileDialog 打开目录文件 保存目录文件
2015-08-29 20:57
507 查看
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
);
参数含义详细说明:
[in] bOpenFileDialog
The parameter that specifies what type of dialog box to create. Set it to TRUE to construct a File Open dialog box. Set it to FALSE to
construct aFile Save As dialog box.
[in] lpszDefExt
The default file name extension. If the user does not include an extension in the Filename box, the extension specified bylpszDefExt is automatically appended to the file name. If this parameter isNULL,
no extension is appended.
[in] lpszFileName
The initial file name that appears in the Filename box. If NULL, no initial file name appears.
[in] dwFlags
A combination of one or more flags that you can use to customize the dialog box. For a description of these flags, see theOPENFILENAMEstructure
in the Windows SDK. If you modify them_ofn.Flags structure member, use a bitwise-OR operator in your changes to keep the default behavior intact.
[in] lpszFilter
A series of string pairs that specify filters you can apply to the file. If you specify file filters, only files that match filter criteria will appear in the Files list. See the Remarks section for more information about how to work with file filters.
[in] pParentWnd
A pointer to the parent or owner window of the file dialog box.
[in] dwSize
The size of the OPENFILENAME structure. This value depends on the operating system version. MFC used this parameter to determine the appropriate kind of dialog box to create (for example, new Windows 2000 dialog boxes instead of NT4 dialog
boxes). The default size of 0 means that the MFC code will determine the correct dialog box size to use based on the operating system version on which the program is run.
[in] bVistaStyle
Note This parameter is applicable only if you are compiling in Windows Vista.
The parameter that specifies the style of the file dialog. Set it to TRUE to use the new Vista style file dialogs. Otherwise, the old style of dialog boxes will be used. See the Remarks section for more information
about compiling under Vista.
Remarks
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 打开文件:
[c-sharp] view
plaincopy
// Create dialog to open multiple files.
CFileDialog dlg(TRUE, _T("txt"), _T("*.txt"), OFN_ALLOWMULTISELECT);
// Create buffer for file names.
const DWORD numberOfFileNames = 100;
const DWORD fileNameMaxLength = MAX_PATH + 1;
1aa70
const DWORD bufferSize = (numberOfFileNames * fileNameMaxLength) + 1;
TCHAR* filenamesBuffer = new TCHAR[bufferSize];
// Initialize beginning and end of buffer.
filenamesBuffer[0] = NULL;
filenamesBuffer[bufferSize-1] = NULL;
// Attach buffer to OPENFILENAME member.
dlg.m_ofn.lpstrFile = filenamesBuffer;
dlg.m_ofn.nMaxFile = bufferSize;
// Create array for file names.
CString fileNameArray[numberOfFileNames];
if(dlg.DoModal() == IDOK)
{
// Retrieve file name(s).
POSITION fileNamesPosition = dlg.GetStartPosition();
int iCtr = 0;
while(fileNamesPosition != NULL)
{
fileNameArray[iCtr] = dlg.GetNextPathName(fileNamesPosition);
iCtr++;
}
}
// Release file names buffer.
delete[] filenamesBuffer;
实例2 打开文件:
[c-sharp] view
plaincopy
CFileDialog dlg(
TRUE,
"*",
"*.xyz",
OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,
"All Files(*.xyz|*.xyz||"
);
char szbuffer[1024];
szbuffer[0]=0;
dlg.m_ofn.lpstrFile = szbuffer;
dlg.m_ofn.nMaxFile = 1024;
if(IDOK==dlg.DoModal())
{
POSITION pos = dlg.GetStartPosition();
CString filepath;
while(pos!=NULL)
{
filepath = dlg.GetNextPathName(pos);
}
}
实例3 打开文件:
[c-sharp] view
plaincopy
CString filePath;
char fileName[256];
char filter[] = "GEO Files(*.GEO)|*.GEO|All Files(*.*)|*.*||";
UpdateData(TRUE);
CFileDialog fdlg(TRUE, "bmp", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter );
strcpy(FileName, _T("文件"));
if ( IDOK != cf.DoModal()) return;
filePath = fdlg.GetPathName(); // filePath即为所打开的文件的路径
UpdateData(FALSE);
实例4 打开文件,并设置对话框标题
[cpp] view
plaincopy
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 保存文件:
[c-sharp] view
plaincopy
char FileName[256];
CString Title, FmtString;
CString PathName;
CString path_and_fileName;
UpdateData(TRUE);
PathName=_T("path.xml");
char BASED_CODE szFilter[] = "XML Files(*.xml)|*.XML|All Files(*.*)|*.*||";
CFileDialog fdlg(FALSE, "XML", PathName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter );
strcpy(FileName, _T("文件名"));
if ( IDOK != fdlg.DoModal() ) return;
path_and_fileName= fdlg.GetPathName(); //path_and_fileName即为文件保存路径
UpdateData(FALSE);
以上是使用 CFileDialog打开文件,下面是使用SHBrowseForFolder打开路径:
[cpp] view
plaincopy
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;
}
}
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
);
参数含义详细说明:
[in] bOpenFileDialog
The parameter that specifies what type of dialog box to create. Set it to TRUE to construct a File Open dialog box. Set it to FALSE to
construct aFile Save As dialog box.
[in] lpszDefExt
The default file name extension. If the user does not include an extension in the Filename box, the extension specified bylpszDefExt is automatically appended to the file name. If this parameter isNULL,
no extension is appended.
[in] lpszFileName
The initial file name that appears in the Filename box. If NULL, no initial file name appears.
[in] dwFlags
A combination of one or more flags that you can use to customize the dialog box. For a description of these flags, see theOPENFILENAMEstructure
in the Windows SDK. If you modify them_ofn.Flags structure member, use a bitwise-OR operator in your changes to keep the default behavior intact.
[in] lpszFilter
A series of string pairs that specify filters you can apply to the file. If you specify file filters, only files that match filter criteria will appear in the Files list. See the Remarks section for more information about how to work with file filters.
[in] pParentWnd
A pointer to the parent or owner window of the file dialog box.
[in] dwSize
The size of the OPENFILENAME structure. This value depends on the operating system version. MFC used this parameter to determine the appropriate kind of dialog box to create (for example, new Windows 2000 dialog boxes instead of NT4 dialog
boxes). The default size of 0 means that the MFC code will determine the correct dialog box size to use based on the operating system version on which the program is run.
[in] bVistaStyle
Note This parameter is applicable only if you are compiling in Windows Vista.
The parameter that specifies the style of the file dialog. Set it to TRUE to use the new Vista style file dialogs. Otherwise, the old style of dialog boxes will be used. See the Remarks section for more information
about compiling under Vista.
Remarks
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 打开文件:
[c-sharp] view
plaincopy
// Create dialog to open multiple files.
CFileDialog dlg(TRUE, _T("txt"), _T("*.txt"), OFN_ALLOWMULTISELECT);
// Create buffer for file names.
const DWORD numberOfFileNames = 100;
const DWORD fileNameMaxLength = MAX_PATH + 1;
1aa70
const DWORD bufferSize = (numberOfFileNames * fileNameMaxLength) + 1;
TCHAR* filenamesBuffer = new TCHAR[bufferSize];
// Initialize beginning and end of buffer.
filenamesBuffer[0] = NULL;
filenamesBuffer[bufferSize-1] = NULL;
// Attach buffer to OPENFILENAME member.
dlg.m_ofn.lpstrFile = filenamesBuffer;
dlg.m_ofn.nMaxFile = bufferSize;
// Create array for file names.
CString fileNameArray[numberOfFileNames];
if(dlg.DoModal() == IDOK)
{
// Retrieve file name(s).
POSITION fileNamesPosition = dlg.GetStartPosition();
int iCtr = 0;
while(fileNamesPosition != NULL)
{
fileNameArray[iCtr] = dlg.GetNextPathName(fileNamesPosition);
iCtr++;
}
}
// Release file names buffer.
delete[] filenamesBuffer;
实例2 打开文件:
[c-sharp] view
plaincopy
CFileDialog dlg(
TRUE,
"*",
"*.xyz",
OFN_FILEMUSTEXIST|OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,
"All Files(*.xyz|*.xyz||"
);
char szbuffer[1024];
szbuffer[0]=0;
dlg.m_ofn.lpstrFile = szbuffer;
dlg.m_ofn.nMaxFile = 1024;
if(IDOK==dlg.DoModal())
{
POSITION pos = dlg.GetStartPosition();
CString filepath;
while(pos!=NULL)
{
filepath = dlg.GetNextPathName(pos);
}
}
实例3 打开文件:
[c-sharp] view
plaincopy
CString filePath;
char fileName[256];
char filter[] = "GEO Files(*.GEO)|*.GEO|All Files(*.*)|*.*||";
UpdateData(TRUE);
CFileDialog fdlg(TRUE, "bmp", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter );
strcpy(FileName, _T("文件"));
if ( IDOK != cf.DoModal()) return;
filePath = fdlg.GetPathName(); // filePath即为所打开的文件的路径
UpdateData(FALSE);
实例4 打开文件,并设置对话框标题
[cpp] view
plaincopy
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 保存文件:
[c-sharp] view
plaincopy
char FileName[256];
CString Title, FmtString;
CString PathName;
CString path_and_fileName;
UpdateData(TRUE);
PathName=_T("path.xml");
char BASED_CODE szFilter[] = "XML Files(*.xml)|*.XML|All Files(*.*)|*.*||";
CFileDialog fdlg(FALSE, "XML", PathName, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter );
strcpy(FileName, _T("文件名"));
if ( IDOK != fdlg.DoModal() ) return;
path_and_fileName= fdlg.GetPathName(); //path_and_fileName即为文件保存路径
UpdateData(FALSE);
以上是使用 CFileDialog打开文件,下面是使用SHBrowseForFolder打开路径:
[cpp] view
plaincopy
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;
}
}
相关文章推荐
- Visual C++中MFC消息的分类
- MFC中Radio Button的用法详解
- MFC对话框中添加状态栏的方法
- MFC创建右键弹出菜单的方法
- MFC中动态创建控件以及事件响应实现方法
- C++ 关于MFC多线程编程的注意事项
- MFC程序对文件的处理方法
- MFC自定义消息的实现方法
- MFC之ComboBox控件用法实例教程
- MFC绘制不规则窗体的方法
- CFileDialog设置多选的问题解决
- IE8引发 VS2005/2008 MFC向导出错的解决方案
- MFC实现全屏功能代码实例
- 使用VS2010创建MFC ActiveX工程项目
- C++中MFC Tab Control控件的使用详解
- MFC程序设计常用技巧汇总
- CFileDialog的钩子函数解决对话框的多选之DoModal问题
- MFC扩展DLL中导出类和对话框的实现方法
- VC中SDK与MFC的区别浅析
- 浅谈MFC 改变控件大小和位置