先来看看参数
Parameters
-
bOpenFileDialog
-
TRUE表示打开文件 FALSE表示另存文件.
-
lpszDefExt
-
文件扩展名. If the user does not include an extension in the Filename edit box, the extension specified by lpszDefExt is automatically appended to the filename. If this parameter is NULL, no file extension is appended.
-
lpszFileName
-
默认文件名。The initial filename that appears in the filename edit box. If NULL, no filename initially appears.
-
dwFlags
-
A combination of one or more flags that allow you to customize the dialog box. For a description of these flags, see the OPENFILENAME structure in the Platform SDK. If you modify the m_ofn.Flags structure member, use a bitwise-OR operator in your changes to keep the default behavior intact.
-
lpszFilter
-
文件过滤器A series of string pairs that specify filters you can apply to the file. If you specify file filters, only selected files will appear in the Files list box. See the Remarks section for more information on how to work with file filters.
-
pParentWnd
-
A pointer to the file dialog-box object's parent or owner window.
-
dwSize
-
The size of the OPENFILENAME structure. This value is dependent on the operating system version, so MFC can determine the appropriate kind of dialog box to create (for example, new Windows 2000 dialogs as opposed to NT4 dialogs). The default size of 0 means that the MFC code will determine the proper dialog box size to use based on the operating system version on which the program is run.
void CChildFrame::OnFileOpen() { // szFilters is a text string that includes two file name filters: // "*.my" for "MyType Files" and "*.*' for "All Files." char CChildFrame::szFilters[]= "MyType Files (*.my)|*.my|All Files (*.*)|*.*||"; // Create an Open dialog; the default file name extension is ".my". CFileDialog fileDlg (TRUE, "my", "*.my", OFN_FILEMUSTEXIST| OFN_HIDEREADONLY, szFilters, this); // 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); } }