MFC uses the System Files dialog box to open files and save files, and to use StdAfx.h to set global variables

Source: Internet
Author: User

In the "MFC" to use the file read and write, Theapp global variables to the real login account management System (click Open link), although also used to open and save the file, but this is when the user opens and closes the dialog box automatically. Now set an "open" button with a "Save" button, you can give users the freedom to choose the location of the saved files, enter the file name, but the saved file format can only be specified by us. Ifo, because later open the file, we only accept. Ifo open. This article is also the "MFC" different dialog between the manipulation, global variables and date control (click to open the link) further work, the last time only the "add" and "delete" button, continue to complete the "open" and "save" button.


I. BASIC OBJECTIVES

As shown, after the user has added some student information, we no longer automatically save the user, of course, here more humane should be when the user clicks the Close button to add a warning pop-up window, here can be written in the OnCancel function. Users save their own information manually, and then the next time they enter this screen, they can open their last saved information. The saved file format is unified. Ifo.


Second, the production process

In fact, mainly on the operation of the CFileDialog, CFileDialog return value can form a specific path to find the file, after the file operation with the "MFC" to use the file read and write, Theapp global variables to the real login account management system (click to open the link) in the same. CFileDialog is the system comes with the class, a definition of direct use can be, nothing difficult, mainly CFileDialog initialization of the parameters set good.

1, because of the file operation, we like the last time, to define a global variable structure, in fact, the definition of global variables not as complex as last time, directly as open StdAfx.h to write global variables, which is the same as the last set Theapp


Write the struct as shown as the global variable:

struct Sinfo{char user[20];//save student name int classnames;//save Student class Char birth[20];//save student's date of birth};

The student date here is a time type, but it can be converted to a string to save Oh!

2. Then the message map function that opens the button:

void Cinfodlg::onbutton4 () {///Todo:add your control notification handler code here//System File Selection dialog box, with a parameter of true, titled "Open" to hide those only The read//file name does not have an extension, it is automatically saved in the. ifo format and is automatically appended to the file name. ifo//The third parameter is the default file name, and the third parameter is automatically filled in the File Name column//"Student Information (*.IFO) |*.ifo| represents the choice of open file type, student information (*.IFO) is a display of information *.ifo its decision function CFileDialog Dlg (TRUE, "Ifo", "" ", Ofn_hidereadonly," Student Information (*.IFO) |*.ifo| | "); /user Selects "Cancel" in the System File selection dialog, then the DLG. The return value of DoModal () is Idcancelif (Idcancel==dlg. DoModal ()) {return;} Gets the path to the file to manipulate CString Filename=dlg. GetPathName (); What CFile file;//read must match the size of the struct, do not match, either read it or read the error if (!file. Open (filename,cfile::moderead| Cfile::sharedenynone) {AfxMessageBox ("Failed to open file"); return;} First empty the list, in the file read out of something to add to the list control inside the M_list.deleteallitems (); Sinfo info; CString str;//reads something that must match the size of the struct, does not match, either reads or reads the error for (int i=0;file. Read (&info,sizeof (info)) ==sizeof (info); i++) {M_list.insertitem (i,info.user); Str. Format ("%d", info.classnames); M_list.setitemtext (I,1,STR); M_list.setitemtext (I,2,info.birth);} File. Close ();}

3. Finally, the message map function to save the button:

void Cinfodlg::onbutton5 () {//Todo:add your control notification handler code here//System File Selection dialog box, with a parameter of false, titled "Save As"//File The name does not have an extension, it is automatically saved in the. Ifo format, and is automatically appended to the file name. ifo//The third parameter is the default file name, and the third parameter is automatically filled in the File Name column//ofn_overwriteprompt//"Student Information (*.IFO) |*. Ifo| represents the selection of saved file types, student information (*.IFO) is a display of information, *.ifo its decisive role CFileDialog Dlg (FALSE, "Ifo", "", Ofn_overwriteprompt, "Student Information (*.IFO) |*. ifo| All Files (*. *) |*.*| | "); if (Idcancel==dlg. DoModal ()) {return;} Opens a User.dat file for the current directory, and no CString Filename=dlg is created. GetPathName (); CFile file;if (!file. Open (filename,cfile::modecreate| Cfile::modewrite) {AfxMessageBox ("Failed to create file"); return;} Sinfo info;int Ncount=m_list.getitemcount (); for (int i=0;i<ncount;i++) {M_list.getitemtext (i,0,info.user,sizeof ( Info.user)); Info.classnames=atoi (M_list.getitemtext (i,1)); M_list.getitemtext (i,2,info.birth,sizeof (Info.birth) ); file. Write (&info,sizeof (info));} File. Close ();}

Well, at this point, the entire student information management system has been completed. With the "MFC" with the dialog box switch implementation re-login "(click Open link)," "MFC" to take advantage of the file read and write, Theapp global variables to the real login account management System "(Click to open the link)," MFC "Different dialog between the control, global variables and date controls (Click to open link) is a very complete student management system. Although in the network OS today, such a system needs to be more and more rare, but, through the production of this system, really let me learn, how to write a complete Win32 program. Of course, MFC has a lot of features to study later.

MFC uses the System Files dialog box to open files and save files, and to use StdAfx.h to set global variables

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.