Cfiledialog usage (translation msdn)

Source: Internet
Author: User

Encapsulate windows universal files in the cfiledialog class dialog box. The common file dialog box provides a simple method for opening and saving files in the dialog box (and selecting other files), which is consistent with the Windows Standard.

You can use the constructor provided by cfiledialog, Or you can write a dialog box class and constructor from cfiledialog to meet your needs. In both cases, these dialogs are like standard Microsoft basic classes, because they are derived from the ccommondialog class.

To use the cfiledialog object, first create the object using the cfiledialog constructor. The dialog box has been created. You can set or modify the control status of any value initialized in the m_ofn structure or the dialog box. The m_ofn structure type is openfilename. For more information, see the openfilename structure in Platform SDK.

After initialization, the control of the dialog box calls the member function to call domodal to display the dialog box and allows the user to enter the path and file. Whether the user has selected the line (idok) or the cancel (idcancel) button to call domodal to return.

If you call domodal to return idok, you can use the common member function of cfiledialog to retrieve information entered by users.

Cfiledialog includes some protected members, allowing you to share behavior, verify custom processing file name and list box change notifications. These protected members are callback functions, which are not required by most applications because the default processing is completed automatically. These Function Message ing items are unnecessary because they are standard virtual functions.

You can use the Windows commdlgextendederror function to determine whether an error occurs in the initialization of the dialog box and learn more.

The object in cfiledialog is automatically destroyed. It is not necessary to call cdialog: enddialog,

To allow users to select multiple files, call the ofn_allowmultiselect flag set before domodal. You need to provide a buffer for your own file name to accommodate the list returned by multiple file names. To do this, replace you have an allocated buffer pointer m_ofn.lpstrfile, and then build the cfiledialog, but previously call domodal. In addition, you must set the number of characters in the buffer m_ofn.nmaxfile to point to m_ofn.lpstrfile.

Cfiledialog depends on the commdlg. dll file with Windows 3.1 and later versions.

If you get a new class from cfiledialog, you can use a message ing to process any message. To expand the default message processing, a class is derived from cwnd, and a message ing is added to the new class, which is a member function of the new mail. You do not have to provide a hook function custom dialog box.

Function prototype:

 

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

 

Bopenfiledialog:If this parameter is set to true, a file open dialog box or a false file creation dialog box is created as a file storage dialog box.

Lpszdefext:The default file extension. If the user is not included in the file extension editing box, extend the specified lpszdefext to automatically append to the file name. If this parameter is null, no file extension is appended.

 

Lpszfilename:The initial file name. The file name appears in the editing box. If it is null, no file name appears initially.

 

Dwflags:One or more tags allow you to customize the combination of dialog boxes. For instructions on these flags, see the openfilename structure in the Platform SDK. If you modify the m_ofn.flags structure member, use one of your changes or the operator to keep the default behavior unchanged.

Lpszfilter:An array can apply a series of specified filters to this file. If the specified file filter is used, only the selected files are displayed in the file list box. For more information about how to use the file filter, see section.

Pparentwnd:1. pointer to the parent or owner window of the object in the file dialog box.

Dwsize:The size of the openfilename structure. This value is related to the operating system version, so MFC can determine the type of dialog creation (for example, the new Windows 2000 NT4 dialog, rather than the dialog ).

Remarks
Whether it is to open a file or save the file as a dialog box, it depends on the value of bopenfiledialog.

To allow users to select multiple files, call the ofn_allowmultiselect flag set before domodal. You need to provide a buffer for your own file name to accommodate the list returned by multiple file names. To do this, replace you have an allocated buffer pointer m_ofn.lpstrfile, and then build the cfiledialog, but previously call domodal. In addition, you must set the number of characters in the buffer m_ofn.nmaxfile to point to m_ofn.lpstrfile.

To enable you to adjust the style of a Resource Manager dialog box, set the ofn_enablesizing flag. Set this flag if you only need to provide a hook program or custom template. This flag only applies to one resource manager-Style dialog box. The old-style dialog box cannot be resized.

This lpszfilter parameter is used to determine whether to display a file in the file list. The first string in the string describes a pair of filters; the second string indicates that the file extension is used. You can specify multiple extensions with ';' as the separator. The string has two '|' characters at both ends, followed by a null character. You can also use this parameter as a cstring object.

For example, an extension that allows users to open in Microsoft Excel. Xlc configuration (Chart) or file. XLS (worksheet. The Excel filter can be written as follows:

Static char based_code szfilter [] = "Chart files (*. xlc) | *. xlc | worksheet files (*. XLS) | *. XLS | data files (*. xlc ;*. XLS) | *. xlc ;*. XLS | all files (*. *) | *. * | ";

However, note that if you want to use this string to directly update the openfilename structure, you should use a string separated by null characters, '/0', instead of a vertical line (|.

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

 

: Getpathname

Return Value
The complete path of the file.

 

Remarks
File name path, including the path of the entire directory of the file plus the title. For example, getpathname returns the "C:/file/text. dat" file C:/file/text. dat.


If m_ofn.flags has the ofn_allowmultiselect flag settings, this string contains an empty teminated string sequence. The first is the directory path string after the names of all files selected in the selected file group. For this reason, use the getstartposition and getnextpathname member functions to retrieve the name of the next file in the list.

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

Cfiledialog: getfilename

Call this function to retrieve the name of the input file name in the dialog box.

Cstring getfilename () const;

Return Value
File Name

 

Remarks
The file name includes both prefix and extension. For example, getfilename will return the "text. dat" file C:/file/text. dat.

If m_ofn.flags has the ofn_allowmultiselect flag settings, you should call getstartposition and getnextpathname to retrieve a file path name.

 

Certificate ----------------------------------------------------------------------------------------------------------------------------------------------------

Cfiledialog: domodal

Overview of cfiledialog | class member | hierarchy chart | cdialog's: Calls domodal | cfiledialog's: cfiledialog
Call this function to display the Windows universal file dialog box. You can browse files and directories and enter a file name.

 

Return Value
Idok or idcancel. If idcancel is returned, call the Windows commdlgextendederror function to determine whether an error has occurred.

Both idok and idcancel are constants, indicating whether to select the OK or cancel button.

Remarks
If you want to initialize the selection of m_ofn structure members in various file dialogs, you should call domodal before, but construct the objects in the dialog box.

When you click the OK or cancel button in the dialog box, or select the close option from the control menu of the dialog box, the control is returned to the application. You can call other member functions to retrieve the settings or user input in the dialog box.

The domodal called by the virtual function is overwritten by the cdialog class.

 

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

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.