1. Create an MFC project named mytab, select dialog based, and finish.
2. the three controls added by default in the delete dialog box. add the tab Control and set the ID to idc_tabtest in the property. Check bottom in more styles. the variable speed size fills the entire dialog box. The size of tab control on my side is 164x203. add a variable to classwizard named m_tab. the type is ctabctrl.
3. Add the following in the initialization function oninitdialog in the dialog box:Code:
M_tab.insertitem (0, "parameter 1"); // Add parameter 1 Tab
M_tab.insertitem (1, "parameter 2"); // Add the parameter 2 tab
M_tab.insertitem (2, "result"); // Add result Tab
4. in the dialog box, add three dialog box resources named idd_para1, idd_para2, and idd_result. the font is, the font size is 9, the style is child, the border is none, and the width is adjusted to 161. then add the cdialog class cpara1, cpara2, and cresult.
5. In the cmytabdlg class, add three member variables m_para1, m_para2, and m_result to the instances in the three subdialog boxes. The Code is as follows:
Cresult m_result;
Cpara2 m_para2;
Cpara1 m_para1;
6. In the idd_para1 dialog box, add the static text control content as "parameter 1", insert a text box control next to it, and associate it with an int variable named m_npara1 using classwizard;
In the idd_para2 dialog box, add the static text control content as "parameter 2", insert a text box control, and associate it with an int variable named m_npara2 using classwizard;
In the idd_result dialog box, add the static text control content as "result", insert a text box control behind it, and associate it with an int variable named m_nresult;
7. Add the member function int getparavalue () to the cpara1 class as follows:
Int cpara1: getparavalue ()
...{
Return m_npara1;
}
The code for adding the member function int getparavalue () to the cpara2 class is as follows:
Int cpara2: getparavalue ()
...{
Return m_npara2;
}
The code for adding the member function void setresultvalue (INT nresult) to the cresult class is as follows:
Void cresult: setresultvalue (INT nresult)
...{
M_nresult = nresult;
}
8. Add the following code to the oninitdialog initialization function in the idd_mytab_dialog dialog box:
// In the join dialog box, set the idc_tabtest control as the parent window.
M_para1.create (idd_para1, getdlgitem (idc_tabtest ));
M_para2.create (idd_para2, getdlgitem (idc_tabtest ));
M_result.create (idd_result, getdlgitem (idc_tabtest ));
// Obtain the size of the idc_tabtest client Zone
Crect RS;
M_tab.getclientrect (& RS );
// Adjust the position of the subdialog box in the parent window
Rs. Top + = 1;
Rs. Bottom-= 60;
Rs. Left + = 1;
Rs. Right-= 2;
// Set the size of the subdialog box and move it to the specified position
M_para1.movewindow (& RS );
M_para2.movewindow (& RS );
M_result.movewindow (& RS );
// Hide and display
M_para1.showwindow (true );
M_para2.showwindow (false );
M_result.showwindow (false );
// Set the default Tab
M_tab.setcursel (0 );
9. Add the tcn_selchange Event Response Function onselchangetabtest (nmhdr * pnmhdr, lresult * presult) of the tab control. The function body code is as follows:
Int cursel = m_tab.getcursel ();
Switch (cursel)
...{
Case 0:
M_para1.showwindow (true );
M_para2.showwindow (false );
M_result.showwindow (false );
Break;
Case 1:
M_para1.showwindow (false );
M_para2.showwindow (true );
M_result.showwindow (false );
Break;
Case 2:
M_para1.showwindow (false );
M_para2.showwindow (false );
M_result.showwindow (true );
Break;
Default:
;
}
* Presult = 0;
10. In the idd_mytab_dialog dialog box, add a button titled "computing" to add an event response function. The Code is as follows:
M_para1.updatedata (true );
M_para2.updatedata (true );
M_result.setresultvalue (m_para1.getparavalue () + m_para2.getparavalue ());
M_result.updatedata (false );
11. The final demonstration result is as follows:
12. This smallProgramIt is very simple, but it illustrates the basic usage of the tab control.
Usage 2
The ctabctrl usage in VC is very different from that in tabs controls of VB and Delphi. Each attribute page is implemented using a "form" (dialog box, therefore, it is much more troublesome to create a class for each property page dialog box, and to associate and initialize it. However, ctabctrl is more reusable than ActiveX tab controls, because one property page can be called by multiple tabs and dialogs, just like in MFC, many property tabs have the "General" option page, which can be called by the property pages of multiple controls as "General, meet the reusability requirements of modern software engineering.
The following is the simplest code. Use the ctabctrl control to implement the property page function.
1. Create a dialog box-based application;
2. Draw the ctabctrl control. In the Class Wizard, the associated variable name is m_tab. Set the properties of the three new dialog boxes to child and none. Use classwizard to generate a new class. The base class is cdialog, cpage0, cpage1, cpage2, and idnumber are idd_dialog0, idd_dialog1, and idd_dialog2 respectively. Add three variables in the Main Dialog Box, cpage0 page0; cpage1 page1; cpage2 page2. Do not forget to add # include "page0.h", # include "page1.h", # include "page2.h" to the header file of the Main Dialog Box"
3. initialize in oninitdialog () of the Main Dialog Box:
// Todo: add extra initialization here
// Initialize the m_tab Control
M_tab.insertitem (0, "Haha, maaoye Studio ");
M_tab.insertitem (1, "");
M_tab.insertitem (2, "Haha, www.maoyeah.com ");
// Create an attribute page
Page0.create (idd_dialog0, getdlgitem (idc_tab1 ));
Page1.create (idd_dialog1, getdlgitem (idc_tab1 ));
Page2.create (idd_dialog2, getdlgitem (idc_tab1 ));
// Set the page location within the m_tab control range
Crect rect;
M_tab.getclientrect (& rect );
Rect. Top + = 20;
Rect. Bottom-= 5;
Rect. Left + = 5;
Rect. Right-= 5;
Page0.movewindow (& rect );
Page1.movewindow (& rect );
Page2.movewindow (& rect );
Page1.showwindow (true );
M_tab.setcursel (1 );
4. When selecting the properties page of The m_tab control, each page is displayed:
Void cmy3dlg: onselchangetab1 (nmhdr * pnmhdr, lresult * presult)
{
// Todo: add your control notification handler code here
Int cursel;
Cursel = m_tab.getcursel ();
Switch (cursel)
{
Case 0:
Page0.showwindow (true );
Page1.showwindow (false );
Page2.showwindow (false );
Break;
Case 1:
Page0.showwindow (false );
Page1.showwindow (true );
Page2.showwindow (false );
Break;
Case 2:
Page0.showwindow (false );
Page1.showwindow (false );
Page2.showwindow (true );
Break;
Default :;
}
* Presult = 0;
}