First of all, improve the last time in the "MFC" using the file read and write, Theapp global variables to the real login account management system (click the Open link) mentioned in one of the methods: in order to solve the MFC modal dialog box in the taskbar does not show the flaw, So in the OnInitDialog () initialization function in some modal dialogs, use SetWindowLong (This->getsafehwnd (), Gwl_exstyle, Ws_ex_appwindow); This can be achieved, but in each modal dialog box to add this sentence is not exhausted? In fact, the solution is as follows:
Open the new MFC project created by the Cxxapp class, the InitInstance () in its own m_pmainwnd=&dlg; deleted or commented out, after all modal dialogs are hidden in the original dialog box, there will be a title. This sentence is present by default. It doesn't take into account that when we use modal dialogs, we hide the main dialog box.
This period, mainly for everyone in MFC how to implement the different dialog between the control and the definition of global variables, as well as the use of the date control, the last time in 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 project, there are " Student Management "This button is not finished, first in the Cisysdlg main dialog box to add Button3 Student management of the message map function, this is the same as the previous project, even the code is the same, do not say:
#include "InfoDlg.h" void Cisysdlg::onbutton3 () {ShowWindow (sw_hide); Cinfodlg Dlg;dlg. DoModal (); ShowWindow (sw_show);}
Among them, the class name added by the dialog we drew in a moment is cinfodlg.
I. BASIC OBJECTIVES
To achieve the following functions, student Information Management, click the Add button will pop up a modal dialog "add Information", which has a name, class, date of birth, where the name is not allowed the same, and then, in the "Add Information" dialog box click the "Add" button, the message will be inserted into the Student Information Management dialog box. The Add Information dialog box uses the edit box, drop-down list, and date control, which can be entered and selected by the date control. In the Student Information Management dialog box, when the information is selected, tapping Delete button will delete the message.
Second, the production process
1, first draw the Student Information Management dialog box, how to add in the "MFC" with the dialog page implementation User Login "(click Open link) said, which contains four buttons and list control, note that the list control is set to a single selection, with the two" OK "," Cancel "button to delete themselves, the main implementation of this issue" Add "and" delete button function, for this dialog box add Class Cinfodlg, "open" and "save" to do after, the button shortcut is how to add, how to add a class for the dialog box, "MFC" to take advantage of the file read and write, Theapp global variables to Real login account management System " (Click to open link) has said.
2. Then draw the "Add Info" control, which contains three static text, an edit box, a drop-down list, and a date control to add the Cinfoapp class to the dialog box.
3. Date control as added:
4, the style of the date control is changed to the following, the format is "Long Date", the other default on the line, you choose "Use Rotation control" also line, personal preferences bar, anyway after the code is not different, personally think the default style is more beautiful. Date controls can be entered by default, and they can choose their own dates.
5, then began to write code, because we first want the "Student Information Management" dialog box in the list control initialization, so we want to add the initialization OnInitDialog () function for Cinfodlg, and we do not need to do this list every time to declare clistctrl* Plist= (clistctrl*) GetDlgItem (Idc_list1), and then the Plist-> method to operate, so we first add a Cinfodlg header file in the clistctrl*
‘
Then go back to OnInitDialog () to use a sentence M_list.subclassdlgitem (idc_list1,this), and later in all of the functions of this dialog, you can use M_list to manipulate the list control. Initialize the list control method, and the previous "MFC" Student information management, the implementation of the list control node additions and deletions (click to open the link), just like no longer plist-> to call, but the members of the M_list.
BOOL Cinfodlg::oninitdialog () {cdialog::oninitdialog ();//Todo:add Extra initialization here// Now log in to a CListCtrl m_list;//in the header file of this dialog box then, you can use this method to get the ID of one of its controls using the//do not have to use the declared clistctrl* plist= (clistctrl*) GetDlgItem each time (idc_ LIST1); Plist-> method to operate M_list.subclassdlgitem (Idc_list1,this); M_list.insertcolumn (0, "name", 0,100); m_ List.insertcolumn (1, "Class", 0,100);//Birth date column expands some m_list.insertcolumn (2, "date of birth", 0,200); return TRUE; Return TRUE unless you set the focus to a control //Exception:ocx property Pages should return FALSE}
Date of birth that column expands a little, after all the date is relatively large
6, after the "Student Information Management" dialog box, the "Add" button of the message map function, this button is pop-up a cinfoadddlg modal box, in order to be able to operate in the "Add" dialog box "Student Information management" Control in this dialog box, Let's start by opening the CInfoAddDlg.h.
Then add the message map function OnButton1 () to the Add button in the Student Information Management dialog box:
#include "InfoAddDlg.h" void Cinfodlg::onbutton1 () {//Todo:add your control notification handler code HERECINFOADDDLG DLG ///In the header file InfoAddDlg.h of the dialog that added the information, a pointer to this dialog box is added//so that the dialog box for adding information can manipulate the Listdlg.m_infodlg=this;dlg in this dialog box. DoModal ();}
7, "Student Information Management" dialog box "delete" button, write the entire Add dialog box and then back to write, the "Add" dialog Box "drop-down list" is to be initialized, for this dialog box to add an initialization OnInitDialog () function, the "drop-down list" in the initialization of " MFC "The use of file read and write, Theapp global variables to the real login account management System" (Click to open the link) is the same, just "normal", "advanced" into the "1", "2" only
BOOL Cinfoadddlg::oninitdialog () {cdialog::oninitdialog ();//Initialize drop-down list combo control ccombobox* pcombo= (ccombobox*) GetDlgItem (IDC_COMBO1);p combo->addstring ("1");p combo->addstring ("2");//Set drop-down list combo control default option for first pcombo-> SetCurSel (0);//Todo:add extra initialization herereturn TRUE; Return TRUE unless you set the focus to a control //Exception:ocx property Pages should return FALSE}
8. Then the Add dialog box has only one Add button that requires a write function:
void Cinfoadddlg::onbutton1 () {///After the previous work, a m_infodlg->xx, just as in the Cinfodlg dialog to manipulate the dialog box control; CString str; GetDlgItemText (IDC_EDIT1,STR);//compare strings must be compared, not with ==if (strcmp (str, "") ==0) {AfxMessageBox ("Enter the name blank!) "); return;} int Ncount=m_infodlg->m_list.getitemcount (); for (int i=0;i<ncount;i++) {if (strcmp (m_infodlg->m_ List.getitemtext (i,0), str) ==0) {AfxMessageBox ("the user name to be inserted already exists! "); return;}} M_infodlg->m_list.insertitem (NCOUNT,STR); GetDlgItemText (IDC_COMBO1,STR); M_infodlg->m_list.setitemtext (NCOUNT,1,STR); GetDlgItemText (IDC_DATETIMEPICKER1,STR); M_infodlg->m_list.setitemtext (NCOUNT,2,STR); Setdlgitemtext (Idc_edit1, "");}
Here is also the previous "add" method, just take out the drop-down list and the date control, not as complex as before, in fact, you can use the GetDlgItemText method to fetch. Note here the method of comparing strings, C + + and Java, the string is an object, can not be directly compared with the basic type of symbol = = to compare, with = more funny, your base is more fun. Also pay attention to the list control, add a row unified with the method of modifying the column to complete the "add", in fact, add a method, one at a time to generate a row, not the assigned column is set to empty.
9, this completes the "Add" dialog box, back to the "Student Information Management" dialog box "Delete" button, in fact, the code here and "MFC" Student information management, the implementation of the list control node additions and deletions (click to open the link), do not have to traverse, casually delete
void Cinfodlg::onbutton2 () {POSITION pos=m_list.getfirstselecteditemposition (); int Nsel=m_list.getnextselecteditem (POS); if (nsel<0) AfxMessageBox ("Please select the item you want to delete! ", MB_OK); Else{if (AfxMessageBox (" Confirm Delete: "+m_list.getitemtext (nsel,0) +"?) ", Mb_yesno) ==idyes) {M_list.deleteitem (Nsel);}} Todo:add your control notification handler code here}
At this point, the goal is complete
"MFC" Different dialogs are manipulated between each other, global variables and date controls