Detailed instructions for the use of MFC's TAB control

                                         Detailed instructions for the use of MFC's TAB control

      Using Tab Control to switch between different interfaces in MFC is a bit difficult for novices to use. I have studied for a day. The following will introduce the problems I encountered and the steps in detail.

1.1 Create an MFC dialog box application, drag the Tab Control control in the dialog box, the effect is as follows:

Note: The tab label needs to be stretched up, down, left and right to the maximum, because the subsequent views need to be painted on it. . .

2. In the header file of the dialog box class, declare a CTabCtrl variable:

                              CTabCtrl m_tab;

The variable m_tab is used to interact with the Tab Control control in the dialog box.

void CMFCApplication2Dlg::DoDataExchange(CDataExchangepDX)
(Build it yourself, not the system itself)

Add DDX_Control statement to the function:

DDX_Control(pDX, IDC_TAB1, m_tab); Note: IDC_TAB1 is the ID of the tab control

3. Insert two dialog boxes in the resource as the interface for Tab Control control switching, and then you must set the Border property of these two dialog boxes to None and Style to Child. The effect after setting is shown in the following figure:

 

 

4. Create two classes for the two dialogs created, such as CTestDlg1 and CTestDlg2. The method of adding a class: right-click the dialog interface and select Add Class in the pop-up box;

 

5. In the header file of the dialog box where the Tab Control control is located, add the following content and add two header files to increase the page:

public:  
        int m_CurSelTab;  

       CTestDlg1 m_page1;   

        CTestDlg2 m_page2;  

        CDialog* pDialog[2]; 

6. In the initialization function of the dialog box class, you need to associate CTestDlg1, CTestDlg2 and Tab Control, save the page address, set the initial page, and add the following implementation code in OnInitDialog():

m_tab.InsertItem(0,  _T ( "Test 1" ));
	m_tab.InsertItem(1,  _T ( "Test 2" ));
	m_tab.InsertItem(2,  _T ( "Test 3" ));
 
	//Create two dialog boxes 
	m_page1.Create( IDD_DIALOG1 , &m_tab);
	m_page2.Create(IDD_DIALOG2, &m_tab);
	m_page3.Create( IDD_DIALOG3 , &m_tab);
	 //Set the range displayed in the Tab 
	CRect  rc;
	m_tab.GetClientRect(rc);
	rc.top += 20;
	rc.bottom -= 0;
	rc.left += 0;
	rc.right -= 0;
	m_page1.MoveWindow(&rc);
	m_page2.MoveWindow(&rc);
	m_page3.MoveWindow(&rc);
 
	//Save the dialog object pointer
	pDialog[0] = &m_page1;
	pDialog[1] = &m_page2;
	pDialog[2] = &m_page3;
	//Display the initial page 
	pDialog[0]->ShowWindow( SW_SHOW );
	pDialog[1]->ShowWindow(SW_HIDE);
	pDialog[2]->ShowWindow( SW_HIDE );
	 //Save the current selection 
	m_CurSelTab = 0;
 
8. Add a message handler for the Tab Control, double-click the Tab Control control, and automatically enter the message handler code:
pDialog[m_CurSelTab]->ShowWindow( SW_HIDE );
         //Get the new page index
    m_CurSelTab = m_tab.GetCurSel();
        //Display the new page 
        pDialog[m_CurSelTab]->ShowWindow( SW_SHOW );
	* Director  = 0;

 

Guess you like

Origin blog.csdn.net/qq_38204688/article/details/75299989