How to Use MFC Tab control controls in C + + _c language

Source: Internet
Author: User

1. Create a new MFC project, named Mytab, choose Dialog based, then finish.

2. Delete the three controls that are added by default on the dialog box. Add tab control controls and set the ID to idc_tabtest in the property attribute bottom in the more styles. The size of the speed control to make it full of dialog box, my side of the tab control is the final 164x203. Add a variable to it in ClassWizard and the variable is named M_tab. Type is CTabCtrl.

3. Add the following code in the initialization function OnInitDialog of the dialog box:

M_tab. InsertItem (0, "parameter One"); Add parameter one tab 
m_tab. InsertItem (1, "parameter Two"); Add parameter two tab 
M_tab. InsertItem (2, "results");  Add Results tab

4. Add three dialog box resources in the dialog resource, named Idd_para1, Idd_para2, Idd_result, respectively. The font is Arial, the font size is 9, the style is child, the border is none, and the width is adjusted to 161. Then add the corresponding based on CDialog class CPara1, CPara2, Cresult respectively.

5. Add three member variables to the Cmytabdlg class M_para1, M_para2, M_result, which is an instance of three child dialog boxes respectively. The code is as follows:

Cresult M_result; 
CPara2 M_para2; 
CPara1 M_para1; 

6. Add a static text control on the Idd_para1 dialog box to "parameter one" and then insert a text box control, with ClassWizard to associate it with an int variable, named m_npara1;
Add a static text control on the Idd_para2 dialog box to "parameter two" and then insert a text box control, with ClassWizard to associate it with an int variable, named m_npara2;
Add a static text control on the Idd_result dialog box to "result" and then insert a text box control, with ClassWizard to associate it with an int variable, named M_nresult;

7. Add the member function int getparavalue () code for the CPara1 class as follows:

int Cpara1::getparavalue () ... 
{return 
 m_npara1; 
} 

Add a member function int getparavalue () code for the CPara2 class as follows:

int Cpara2::getparavalue () ... 
{return 
 m_npara2; 
} 

Add a member function void Setresultvalue (int nresult) code for the Cresult class as follows:

void Cresult::setresultvalue (int nresult) 
... { 
   m_nresult = nresult; 
} 

8. Add the following code in the initialization function OnInitDialog of the Idd_mytab_dialog dialog box:

association dialog box, and 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 idc_tabtest client area size 
CRect rs; 
M_tab. GetClientRect (&RS); 
Adjusts the position of the Child dialog box in the parent window 
rs.top+=1; 
rs.bottom-=60; 
Rs.left+=1; 
rs.right-=2; 
 
Sets the child dialog box dimensions and moves to the specified location 
m_para1. MoveWindow (&RS); 
M_para2. MoveWindow (&RS); 
M_result. MoveWindow (&RS); 
 
Set the Hide and display 
m_para1 separately. ShowWindow (true); 
M_para2. ShowWindow (false); 
M_result. ShowWindow (false); 
 
Set the default tab 
M_tab. SetCurSel (0);

9. Add the tab control's Tcn_selchange event response function onselchangetabtest (nmhdr* pnmhdr, lresult* pResult), 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. Add a button under the Idd_mytab_dialog dialog box titled "Calculate" to add an event response function for it, 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); 

The above mentioned is not to ask all the content, I hope you can enjoy.

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.