格式说明:

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 theOPENFILENAME
structure 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 打开文件:

// 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;
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 打开文件:

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 打开文件:

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 打开文件,并设置对话框标题

	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  保存文件:

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打开路径:

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;
} }

CFileDialog 打开文件夹文件 保存文件夹文件的更多相关文章

  1. C# 通过物理路径将文件以二进制保存到指定文件夹

    /// <summary> /// 通过物理路径将文件以二进制保存到指定文件夹 /// </summary> /// <param name="filePath ...

  2. springboot-用logback将日志文件按等级保存到不同文件

    springboot-用logback将日志文件按等级保存到不同文件 案例: 例如项目基本包名为com.xxx,将该包下的所有日志按debug.info.warn.error等级分别保存到D:/log ...

  3. C++ 基于STL的演讲比赛流程管理系统(sort算法+小型算法(accumulate)+内建函数对象+string字符串拼接+字符串截取+多个容器基础操作+与用户交互+文件的读写+保存+重建+整体文件数据的清空)

    1 /* 2 比赛规则: 3 学校举行一演讲比赛,共12个人参加,比赛两轮,第一轮为淘汰赛 第二轮为决赛 4 每名选手都有对应的编号:如10001~10012 5 比赛方式:分组比赛 每组6人 6 第 ...

  4. Nodejs Express下载文件,并保存成原文件

    现时需要开发一个Excel下载功能 后台有一个API,负责接收传入的JSON文件,生成带图片的Excel文件在临时目录(生成Excel使用npm exceljs库),并将文件通过Router返回 前台 ...

  5. CAD保存DWG文件,设置保存的文件版本号和密码

    主要用到函数说明: MxDrawXCustomFunction::Mx_SaveDwgEx 保存DWG文件,可以设置保存的文件版本号和密码,详细说明如下: 参数 说明 IN CString sFile ...

  6. input type file onchange上传文件的过程中,同一个文件二次上传无效的问题。

    不要采用删除当前input[type=file]这个节点,然后再重新创建dom这种方案,这样是不合理的.解释如下:input[type=file]使用的是onchange去做,onchange监听的为 ...

  7. sublimeText3 中配置sass环境,并将编译后文件保存到指定文件夹

    sass基于ruby引擎,所以安装时ass.compass之前需要安装ruby.具体的链接应该是(http://rubyinstaller.org/downloads).下载并安装相应的版本,勾选第二 ...

  8. C# 选择文件、选择文件夹、打开文件(或者文件夹) 路径中获取文件全路径、目录、扩展名、文件名称 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名!!

    https://www.cnblogs.com/zhlziliaoku/p/5241097.html 1.选择文件用OpenDialog OpenFileDialog dialog = new Ope ...

  9. [转载]Windows系统的错误报告保存在哪个文件夹里?

    转自:http://www.xitonghe.com/jiaocheng/xp-786.html   Windows系统的错误报告保存在哪个文件夹里? 发布时间:2014-10-31 20:52:20 ...

随机推荐

  1. hdu 5637 Transform 最短路

    题目链接 异或的性质. 求s到t的最少步骤, 等价于求0到s^t的最少步骤. 通过最少的步骤达到s^t的状态, 等价于求0到s^t的最短路. 先将最短路求出来然后O(1)查询. #include &l ...

  2. QT5的中文路径和目录问题小记

    今天重新整理了磁盘文件后 使用qt发现编译不过 提示找不到工程的pro文件 原因:我把原来的qt工作目录删掉了 导致qt默认找“我的文档” 作为工作目录 而中文路径导致了这个问题,MARK之 /// ...

  3. android select选择器 checkbox改外观,button按下状态

    android 可以用选择器.来加载视图.选择器里的选项也很多针对实际使用中用的几个进行描述. 1.button 的按下弹起改外观.选择器属性用 android:state_pressed   2.C ...

  4. 权威验证:MSDN会明确告诉你下载的光盘镜像是否正宗微软原版

    MSDN是微软官方网站.这个网站的职能之一,就是向MSDN订户(付费相当高昂)提供Microsoft Windows资源,即大家通常说的操作系统光盘镜像.相信大家手头都有不少这类下载,但究竟是否微软的 ...

  5. ios jsbrige

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. c++游戏编程书籍

    如果要自学游戏程序开发的话,可以看看下面的,呵呵. 游戏开发资料(PDF书都是中文版的,非英文,很多是本人自己扫描制作,从未网上发布过,所以独家啦):  1.Gamebryo 2.2游戏引擎(盛大.腾 ...

  7. Saiku图表导出时中文显示问题的解决方法

    Saiku图表导出时png,jpg,pdf三种格式的中文显示都有问题,目前找到一种不太完善的解决方法(中文可以显示但不清晰),需要修改Saiku项目下的ExporterResource.java文件, ...

  8. Android 刷新下拉控制 SwipeRefreshLayout

    上个月,google它宣布了自己的下拉刷新控制------SwipeRefreshLayout,控制封装在android-support-v4.jar包裹,依靠听力OnRefreshListener实 ...

  9. 判断一个指定的Service是否存在的方法

    这是一个判断一个指定的Service是否存在的方法.它被用于监视一个Service是否由于已经运转,如果由于各种原因Service已经被停止了.这是在重新启动指定Service.它被用于一个Appli ...

  10. mysql 存储过程 游标的使用 与定义

    1.游标的作用及属性 游标的作用就是用于对查询数据库所返回的记录进行遍历,以便进行相应的操作:游标有下面这些属性: a.游标是只读的,也就是不能更新它: b.游标是不能滚动的,也就是只能在一个方向上进 ...