1 ExplicitCFileDialog (2 BOOL Bopenfiledialog, which type of dialog box is created for the specified parameter. TRUE to set the Open dialog box to the construction file. Sets the FALSE to the Construct Save File dialog box. 3LPCTSTR LPSZDEFEXT =NULL, the default file name extension. If the user does not include a known extension in the File name box that contains the association on the user's computer (one, lpszDefExt the specified extension is automatically appended to the file name.) If this parameter is NULL, the extension is not appended. 4LPCTSTR lpszFileName =NULL to display the initial file name of the file name box. If NULL, the initial file name is not displayed. 5DWORD dwFlags = ofn_hidereadonly |ofn_overwriteprompt, you can use a combination of one or more tags in a custom dialog box. For descriptions of these flags, see. The OPENFILENAME structure of the Windows SDK. If you modify m_ofn. Flags struct members, use the bitwise OR operator to keep the default value behavior unchanged in your changes. 6LPCTSTR Lpszfilter = NULL, a series of string name filters can be applied to a file. If the file filter is specified, only the list of files that meet the filter criteria will appear in the file. Please see"Notes"Some more information about how to install is used with file filters. 7cwnd* pParentWnd =NULL, pointer to the parent or owner window of the file dialog box. 8DWORD dwsize =0,9BOOL Bvistastyle =TRUETen);
1. Save As dialog box
1 voidCmfcapplication_testdlg::onfilewrite ()2 {3 //TODO: Add Command handler code here4 //cfile::modecreate Create the file, and if it already exists, empty the content5 //Cfile::modewrite can write6 7 //Szfilters is a text string that includes some file name filters:8 StaticTCHAR szfilter[] = _t ("Chart Files (*.xlc) |*.xlc|")9_t ("Worksheet Files (*.xls) |*.xls| Data Files (*.xlc;*.xls) |")Ten_t ("*.xlc; *.xls| All Files (*. *) |*.*| |"); OneCFileDialog Filedlg (false,null,null,ofn_hidereadonly |ofn_overwriteprompt,szfilter); AFileDlg.m_ofn.lpstrTitle = _t ("Linden");//setting the caption of a dialog box - //FileDlg.m_ofn.lpstrFilter = _t ("Text Files (*.txt) \0*.txt\0all files (*. *) \0*.*\0\0"); //set filter criteria for a file -FILEDLG.M_OFN.LPSTRDEFEXT = _t ("txt");//Set Default extension the if(IDOK = =Filedlg.domodal ()) { -CFile file (Filedlg.getpathname (), Cfile::modecreate |cfile::modewrite); - CharPbufwrite[] ="Test:abcdefghijklmny"; - file. Write (Pbufwrite,strlen (Pbufwrite)); + file. Close (); - } +}
2. Open File dialog box
1 CFileDialog Filedlg (TRUE);2FileDlg.m_ofn.lpstrFilter = _t ("text file (*.txt) |*.txt| Word file (*.doc) |*.doc| All Files (*. *) |*.*| |");//Set Filter3 if(IDOK = =Filedlg.domodal ()) {4CFile file (Filedlg.getpathname (), cfile::moderead);//getpathname (): Get the full file name, including directory name and extension such as: C:\test\test1.txt5 Char*PBuf;6 DWORD Dwfilelen;7Dwfilelen =file. GetLength ();8PBuf =New Char[Dwfilelen +1];9Pbuf[dwfilelen] =0;Ten file. Read (Pbuf,dwfilelen); One file. Close (); A:: MessageBoxA (0, PBuf,0, MB_OK);//VS2012 Default Project Character set is Unicode -}
VS MFC CFileDialog Class Open File dialog box and Save As dialog box