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

);

參数含义具体说明:

[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 打开文件夹文件 保存文件夹文件

时间: 2024-07-28 16:42:40

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

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

不要采用删除当前input[type=file]这个节点,然后再重新创建dom这种方案,这样是不合理的.解释如下:input[type=file]使用的是onchange去做,onchange监听的为input的value值,只有再内容发生改变的时候去触发,而value在上传文件的时候保存的是文件的内容,你只需要在上传成功的回调里面,将当前input的value值置空即可.event.target.value=''; 转自https://www.cnblogs.com/imsomnus/p/62

文件夹分级保存文件

在上传文件时,文件可能会很多,我们知道,当把全部的文件放到同一个目录中之后,打开目录会很慢 这样也说明了,查找文件的效率是降低的 我们把文件放到不同的目录中,以便于提高文件查找效率 文件夹分级思想 采用16进制命名文件夹,创建二级目录,这样可以把文件放到16*16=256个文件夹里面 1 String str="lkjhgfds.jpg";//文件名 2 int a=str.hashCode();//文件名的哈希值 3 int b=a & 0xf;//取后4位 4 System

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

sass基于ruby引擎,所以安装时ass.compass之前需要安装ruby.具体的链接应该是(http://rubyinstaller.org/downloads).下载并安装相应的版本,勾选第二项(要在cmd中使用ruby). 打开命令行,输入ruby -v,查看我们安装的ruby版本信息. ruby安装完成之后,打开ruby的command面板,接下来就是安装sass了.Windows下安装sass有多种方法,这里说一下其中的两种: 1.到 Rubygems(http://rubygem

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

https://www.cnblogs.com/zhlziliaoku/p/5241097.html 1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以选择多个文件 dialog.Title = "请选择文件夹"; dialog.Filter = "所有文件(*.*)|*.*"; if (dialog.ShowDial

MFC中CFileDialog打开和保存文件对话框(转)

首先我先写一段在VC6.0上打开/保存文件对话框的程序:        CString   FilePathName;//文件名参数定义    CFileDialog  Dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"TXT Files(*.txt)|*.txt|All Files(*.*)|*.*");     //打开文件    if(Dlg.DoModal() == IDOK)//是否打开成功    {   

Temporary ASP.NET Files 文件夹中保存的是什么内容?[转]

转自:http://www.cnblogs.com/suiqirui19872005/archive/2007/05/14/746320.html ASP.NET 页面请求的处理过程需要使用一些临时文件.当您在 Web 服务器上安装 ASP.NET 2.0 时,所创建的文件夹层次结构如下: %WINDOWS%\Microsoft.NET\Framework\v2.0.50727 这里的版本号指的是 ASP.NET 2.0 的零售版.ASP.NET 的每个发布版本(包括每个过渡性的内部版本)都有一

Python - 批量获取文件夹的大小输出为文件格式化保存

很多时候,查看一个文件夹下的每个文件大小可以轻易的做到,因为文件后面就是文件尺寸,但是如果需要查看一个文件夹下面所有的文件夹对应的尺寸,就发现需要把鼠标放到对应的文件夹上,稍等片刻才会出结果. 有时候,我们需要查看几十个甚至于上百个文件夹,找出包含文件最多,空间占用最大的那个,就比较麻烦了.这段代码是我以前的代码,可以按大小排序输出文件夹大小到txt文件,供使用的方便. 格式化当时花了很长时间,最后发现使用'YaHei.Consolas'字体可以解决,对齐后输出结果看起来还算舒服. 上代码: i

(转) Ubuntu 更改文件夹及子文件夹权限

Linux系统下如何修改文档及文件夹(含子文件夹)权限,我们来看一下. 一 介绍: 可以使用命令chmod来为文件或目录赋予权限.Linux/Unix 的档案存取权限分为三级 : 档案拥有者.群组.其他.利用 chmod 可以藉以控制档案如何被他人所存取 二 详解 1 此命令有两种使用方法,一种是chmod后加数字,后接文件名 chmod abc file 其中a,b,c各为一个数字,分别表示User.Group.及Other的权限. r=4,w=2,x=1 若要rwx属性则4+2+1=7: 若

自学整理一:java文件对话框的使用(包括文件多选,文件、文件夹同时可选操作)

老早写的,现在转移阵地 在java中有两种方式调用文件对话框 (1)一种是AWT中的FileDialog类,通过FileDialog类创建一个对象,该对象即是所需要的对话框, 例如,FileDialog dialog=new FileDialog(new Frame(),"选择存放位置",FileDialog.LOAD);   这一行代码使用了FileDialog类的其中一个构造函数 FileDialog(Dialog parent,String title, int mode)