1、首先,截取一段MSDN上的内容:



Call this function to construct a standard Windows file dialog box.




explicit CFileDialog(
   BOOL bOpenFileDialog,
   LPCTSTR lpszDefExt = NULL,
   LPCTSTR lpszFileName = NULL,
   DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
   LPCTSTR lpszFilter = NULL,
   CWnd* pParentWnd = NULL,
   DWORD dwSize = 0,
   BOOL bVistaStyle = TRUE
);


Parameters



 bOpenFileDialog

TRUE to construct a File Open dialog box. Set it to FALSE to construct a File Save As



TRUE表示构造打开对话框,FALSE表示构造保存对话框


[in]  lpszDefExt

lpszDefExt is automatically appended to the file name. If this parameter is NULL, no extension is appended.



默认的文件扩展名:当用户输入文件名,但是没有输入文件扩展名,则以该参数为扩展名,另注:该参数不需包括扩展名前的“.”,例如_T("txt")


[in]  lpszFileName

NULL, no initial file name appears.



初始文件名:即编辑框中的文件名,例如_T("未命名.txt")


[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 the OPENFILENAME structure in the Windows SDK. If you modify the m_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.



OPENFILENAME结构的大小


[in]  bVistaStyle

Note

TRUE



对话框样式



Remarks



File Open or File Save As dialog box is constructed, depending on the value of bOpenFileDialog.

To enable the user to select multiple files, set the OFN_ALLOWMULTISELECT flag before you call DoModal. 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 bym_ofn.lpstrFile. If you set the maximum number of files to be selected to n, the necessary buffer size is n*(_MAX_PATH + 1) + 1. For example:



C++





CFileDialog dlgFile(TRUE);
CString fileName;
const int c_cMaxFiles = 100;
const int c_cbBuffSize = (c_cMaxFiles * (MAX_PATH + 1)) + 1;
dlgFile.GetOFN().lpstrFile = fileName.GetBuffer(c_cbBuffSize);
dlgFile.GetOFN().nMaxFile = c_cbBuffSize;

dlgFile.DoModal();
fileName.ReleaseBuffer();



To enable the user to resize an Explorer-style dialog box by using either the mouse or keyboard, set the OFN_ENABLESIZING flag. Setting this flag is necessary only if you provide a hook procedure or custom template. The flag works only with an Explorer-style dialog box; old-style dialog boxes cannot be resized.

lpszFilter parameter is used to determine the type of file name a file must have to be displayed in the file list. The first string in the string pair describes the filter; the second string indicates the file name extension to use. Multiple extensions may be specified by using a semicolon (the ';' character) as the delimiter. The string ends with two '|' characters, followed by a NULL character. You can also use a CString object for this parameter.

For example, Microsoft Excel allows users to open files that have extensions .xlc (chart) or .xls (worksheet), among others. The filter for Excel could be written as:



C++





static TCHAR BASED_CODE szFilter[] = _T("Chart Files (*.xlc)|*.xlc|")
   _T("Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|")
   _T("*.xlc; *.xls|All Files (*.*)|*.*||");



However, if you plan to use this string to directly update the OPENFILENAME structure, you should delimit your strings with the null character, '\0', instead of the vertical bars ('|').

bVistaStyle parameter is applicable only when running under Windows Vista. Under earlier versions of Windows, this parameter is ignored. If bVistaStyleis set to TRUE, when you compile a program with Visual Studio 2008 or later, the new Vista style File Dialog will be used. Otherwise, the previous MFC styleFile Dialog will be used. See CFileDialog Class for more information.

bVistaStyle





2、接着贴一个示例代码:


Example



C++





void CMyClass::OnFileOpen()
{
   // szFilters is a text string that includes two file name filters:
   // "*.my" for "MyType Files" and "*.*' for "All Files."
   TCHAR szFilters[]= _T("MyType Files (*.my)|*.my|All Files (*.*)|*.*||");

   // Create an Open dialog; the default file name extension is ".my".
   CFileDialog fileDlg(TRUE, _T("my"), _T("*.my"),
      OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

   // Display the file dialog. When user clicks OK, fileDlg.DoModal() 
   // returns IDOK.
   if(fileDlg.DoModal() == IDOK)
   {
      CString pathName = fileDlg.GetPathName();

      // Implement opening and reading file in here.

      //Change the window's title to the opened file's title.
      CString fileName = fileDlg.GetFileTitle();

      SetWindowText(fileName);
   }
}


CFileDialog文件打开(保存)对话框_mfc

Requirements



Header: afxdlgs.h

void OnFileSave() 
{
	// 过滤器(写法一)
	TCHAR szFilters[] = _T("PNG (*.png)|*.png|			\
							JPEG (*.jpg)|*.jpg|			\
							GIF (*.gif)|*.gif|			\
							BMP (*.bmp)|*.bmp|			\
							All Files (*.*)|*.*||");
	// 过滤器(写法二)
	TCHAR szFilters2[] = _T("PNG (*.png)|*.png|")
						 _T("JPEG (*.jpg)|*.jpg|")	
						 _T("GIF (*.gif)|*.gif|")		
						 _T("BMP (*.bmp)|*.bmp|")	
						 _T("All Files (*.*)|*.*||");

	CFileDialog fileDlg (FALSE,				// 保存对话框
		_T("bmp"),							// 默认的文件扩展名
		_T("未命名"),						// 初始文件名
		OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, // 标志
		szFilters2,							// 过滤器
		this);								// 父窗口指针

	if( fileDlg.DoModal ()==IDOK )
	{
		CString pathName = fileDlg.GetPathName();
		CString fileName = fileDlg.GetFileTitle ();

		AfxMessageBox(pathName);
		AfxMessageBox(fileName);
	}
}






Example



C++





void CMyClass::OnFileOpen()
{
   // szFilters is a text string that includes two file name filters:
   // "*.my" for "MyType Files" and "*.*' for "All Files."
   TCHAR szFilters[]= _T("MyType Files (*.my)|*.my|All Files (*.*)|*.*||");

   // Create an Open dialog; the default file name extension is ".my".
   CFileDialog fileDlg(TRUE, _T("my"), _T("*.my"),
      OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);

   // Display the file dialog. When user clicks OK, fileDlg.DoModal() 
   // returns IDOK.
   if(fileDlg.DoModal() == IDOK)
   {
      CString pathName = fileDlg.GetPathName();

      // Implement opening and reading file in here.

      //Change the window's title to the opened file's title.
      CString fileName = fileDlg.GetFileTitle();

      SetWindowText(fileName);
   }
}



Requirements



Header: afxdlgs.h