Detailed resolution of the use of the Tab control control in VC

  • 2020-04-02 01:35:17
  • OfStack

1. Create a new MFC project, name it MyTab, select Dialog based, and then Finish.

2. Add Tab Control Control and set the ID to IDC_TABTEST in the Property Property. Tick Bottom in More Styles. Adjust the size to cover the whole dialog box.

3. Add the following code to the OnInitDialog initialization function:
M_tab.InsertItem(0," parameter 1 ");   // add parameter one TAB
M_tab.InsertItem(1," parameter 2 ");   // add parameter two TAB
M_tab. InsertItem (2, "result");       // add the results TAB

4. In the dialog box resource, add three dialog box resources, ID is named as IDD_PARA1, IDD_PARA2, IDD_RESULT. The font is song style, the font size is 9, the style is Child, the Border is None, the width is 161. Add corresponding cdialog-based classes CPara1, CPara2, CResult respectively.

5. Add three member variables to the CMyTabDlg class, m_para1, m_para2, and m_result, which are instances of each of the three child dialogs.
CResult m_result;
CPara2 m_para2;
CPara1 m_para1;

6. Add a static text control on the IDD_PARA1 dialog as "parameter one" and then insert a text box control, using the ClassWizard to associate it with an int variable called m_nPara1;

Add a static text control on the IDD_PARA2 dialog as "parameter two" and then insert a text box control, using the ClassWizard to associate it with an int variable called m_nPara2;

On the IDD_RESULT dialog box, add a static text control with the content as "result" and insert a text box control after that. Use the ClassWizard to associate it with an int variable named m_nResult.

7. Add the member function int GetParaValue() to the CPara1 class


int CPara1::GetParaValue()
...{
    return m_nPara1;
}

Add the member function int GetParaValue() to the CPara2 class

int CPara2::GetParaValue()
...{
    return m_nPara2;
}

Add the CResult class member function void SetResultValue(int nResult)

void CResult::SetResultValue(int nResult)
...{
     m_nResult= nResult;
}

8. Add the following code to the initialization function OnInitDialog of IDD_MYTAB_DIALOG:

//Associate the dialog box and set the IDC_TABTEST control to 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));

//Gets the IDC_TABTEST client area size
CRect rs;
m_tab.GetClientRect(&rs);
//Adjust 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 size of the child dialog box and moves it to the specified location
m_para1.MoveWindow(&rs);
m_para2.MoveWindow(&rs);
m_result.MoveWindow(&rs);
//Set hide and show respectively
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 Tab Control Control, the function body code is as follows:

int CurSel= m_tab.GetCurSel();
    switch(CurSel)
    ...{
    case0:
         m_para1.ShowWindow(true);
         m_para2.ShowWindow(false);
         m_result.ShowWindow(false);
        break;
    case1:
         m_para1.ShowWindow(false);
         m_para2.ShowWindow(true);
         m_result.ShowWindow(false);
        break;
    case2:
         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 box titled "calculate" to add an event response function,

m_para1.UpdateData(true);
     m_para2.UpdateData(true);
     m_result.SetResultValue(m_para1.GetParaValue()+m_para2.GetParaValue());
     m_result.UpdateData(false); 

11. This small program is simple, but it illustrates the basic use of the Tab Control Control.

Note:
1: // association dialog box, and set the IDC_TABTEST control as the parent window (method 2)
M_para1. SetParent (GetDlgItem (IDC_TABTEST));
M_para2. SetParent (GetDlgItem (IDC_TABTEST));
M_result. SetParent (GetDlgItem (IDC_TABTEST));

2: / / get the size of IDC_TABTEST client area
CRect rs;
M_tab. GetClientRect (& rs);

// adjust the position of the child dialog box in the parent window
Rs. Top + = 1;
Rs. The bottom - = 60;
Rs. Left + = 1;
Rs. Right - = 2;

One thing to note here: adjust the distance, otherwise the child dialog box overrides the Tab control control control and cannot be displayed
This is what will happen:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309161021507.jpg ">

// adjust the position of the child dialog box in the parent window as follows
  Rs. Top + = 20;
  Rs. The bottom - = 20;
  Rs. Left + = 20;
  Rs. Right - = 20;

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/201309161021508.jpg ">

Haha, pay attention.


Related articles: