VS2010/MFC dialog box: File dialog box

Source: Internet
Author: User
Tags save file

File dialog box

The previous section describes the message dialog box, which explains the file dialog box. The file dialog box is also a common type of dialog box.

Classification of File dialog boxes

The file dialog is divided into the Open File dialog box and the Save File dialog box, which I believe you will see frequently in Windows systems. For example, many editing software such as Notepad and so on have "open" option, select "Open" will pop up a dialog box, let us choose the path to open the file, this dialog is open File dialog box, in addition to the "open" option generally there will be "Save as" option, after selecting "Save as" will often have a dialog box popup, Let's choose to save the path, which is the Save File dialog box.

As illustrated above, the Open File dialog box is used to select the path to the file to open, and the Save File dialog box is used to select the path of the file to be saved.

file dialog box class CFileDialog

MFC uses the file dialog class CFileDialog to encapsulate the actions of the file dialog box. The constructor prototypes for the CFileDialog class are as follows:

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

Parameter description:

Bopenfiledialog: Specifies the type of file dialog box to create. Set to True to create the Open File dialog box, or the Save File dialog box will be created.

LPSZDEFEXT: The default file name extension. If the user does not enter an extension in the File name edit box, the extension specified by Lpszdefext is automatically added to the file name. The default is null.

lpszFileName: The initial file name displayed in the filename edit box. If NULL, the initial file name is not displayed.

DwFlags: The properties of a file dialog box, which can be a value or a combination of multiple values. With regard to the definition of a property value, you can find the struct openfilename in MSDN, and the description of the element flags contains all the property values. The default is a combination of ofn_hidereadonly and Ofn_overwriteprompt, ofn_hidereadonly represents the Read only check box on the Hidden File dialog box, Ofn_ Overwriteprompt means that in the Save File dialog box, if the file you selected exists, a message dialog box pops up asking you to determine if you want to overwrite the file.

Lpszfilter: File filter, which is a sequence of strings consisting of several string pairs. If a file filter is specified, only files that meet the filter criteria in the file dialog box appear in the file list to be selected. Let's take a look at an example given in VS2010 MSDN:

Static TCHAR based_code szfilter[] = _t ("Chart Files (*.XLC) |*.xlc| Worksheet Files (*.xls) |*.xls| Data Files (*.xlc;*.xls) |*.xlc; *.xls| All Files (*. *) |*.*| | ");

After you set up the filter, there are four options in the file dialog box's extension combo box: Chart files (*.xlc), Worksheet files (*.xls), Data Files (*.xlc;*.xls), and all files (*. *), You can see that each file's extension is defined as a string pair, such as the filter string for chart files, which appears in pairs of chart files (*.XLC) and *.XLC.

pParentWnd: Pointer to the parent window of the file dialog box.

Dwsize:openfilename the size of the structure body. Different operating systems correspond to different dwsize values. MFC uses this parameter to determine the appropriate type of File dialog box (for example, to create a Windows 2000 File dialog box or an XP file dialog box). The default is 0, which means that MFC determines which file dialog box to use based on the version of the operating system that the program is running.

Bvistastyle: Specifies the style of the file dialog box, set to True to use the Vista-style File dialog box, otherwise use the old version of the file dialog box. This parameter is only applicable when compiling in Windows Vista.

The file dialog box is also modal dialog, so you need to call the DoModal () member function of the CFileDialog class when you open it. After clicking "Open" in the Open File dialog box or clicking "Save" in the Save file dialog, we can get the selected file path using the CFileDialog class's member function GetPathName ().

Here are a few of the member functions of the CFileDialog class that we can use to get the various options in the file dialog box.

Getfileext (): Gets the suffix name of the selected file.
GetFileName (): Gets the name of the selected file, including the suffix name.
GetFileTitle (): Gets the title of the selected file, which does not include the suffix name.
GetFolderPath (): Gets the directory of the selected file.
Getnextpathname (): Gets the full path of the next selected file.
GetPathName (): Gets the full name of the path for the selected file.
Getreadonlypref (): Gets whether "open as read-only".
GetStartPosition (): Gets the position of the first element in the list of file names.

file dialog Box Instance

According to the previous content, we will make a file dialog box instance.

1. Create a dialog-based MFC application project with the name "Example17".

2. Modify the Idd_example17_dialog template of the main dialog box to delete the automatically generated Todo:place dialog controls here. Static text box, add two edit boxes, IDs Idc_open_edit and Idc_save_edit respectively, add two buttons, the IDs are set to Idc_open_button and Idc_save_button,caption respectively to "open" and " Save ". The button Idc_open_button is used to display the Open File dialog box, and the edit box idc_open_edit displays the file path selected in the Open File dialog box. The button Idc_save_button is used to display the Save File dialog box, and the edit box Idc_save_button displays the file path selected in the Save File dialog box.

3. Add the message handler function Cexample17dlg::onbnclickedopenbutton () and Cexample17dlg::o for the button Idc_open_button and Idc_save_button respectively for the click Message Nbnclickedsavebutton ().

4. Modify the two message processing functions as follows:

C + + code
  1. void Cexample17dlg::onbnclickedopenbutton ()
  2. {
  3. //Todo:add your control notification handler code here
  4. //Set filter
  5. TCHAR szfilter[] = _t ("text file (*.txt) |*.txt| All Files (*. *) |*.*| |");
  6. //Construct Open File dialog box
  7. CFileDialog Filedlg (TRUE, _t ("txt"), NULL, 0, Szfilter, this );
  8. CString strFilePath;
  9. //Show Open File dialog box
  10. if (IDOK = = Filedlg.domodal ())
  11. {
  12. //If you click on the "Open" button on the file dialog, the selected file path is displayed in the edit box
  13. strFilePath = Filedlg.getpathname ();
  14. Setdlgitemtext (Idc_open_edit, strFilePath);
  15. }
  16. }
  17. void Cexample17dlg::onbnclickedsavebutton ()
  18. {
  19. //Todo:add your control notification handler code here
  20. //Set filter
  21. TCHAR szfilter[] = _t ("text file (*.txt) |*.txt| Word file (*.doc) |*.doc| All Files (*. *) |*.*| | ");
  22. //Construct Save File dialog box
  23. CFileDialog Filedlg (FALSE, _t ("Doc"), _t ("my"), Ofn_hidereadonly |   Ofn_overwriteprompt, Szfilter, this );
  24. CString strFilePath;
  25. //Show Save file dialog box
  26. if (IDOK = = Filedlg.domodal ())
  27. {
  28. //If you click on the "Save" button on the file dialog, the selected file path is displayed in the edit box
  29. strFilePath = Filedlg.getpathname ();
  30. Setdlgitemtext (Idc_save_edit, strFilePath);
  31. }
  32. }

The Windows API function Setdlgitemtext is used when the contents of the edit box are displayed, but you can also associate variables with the edit box before using the
Cdialogex::updatedata () function, but more accustomed to use the Setdlgitemtext function, feel more flexible.

5. Run this program and click on the "Open" button on the Results dialog box to display the Open File dialog box as follows:

After clicking the Save button, the Save File dialog box appears:

After the file path is selected in the Open File dialog box and the Save File dialog box, the main dialog box is as follows:

Here, the file dialog box is finished.

Vs2010/mfc dialog box: File dialog box

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.