Foreword: My development environment here is VS2010. Other development environments may be different, but they will never be too different. The fundamental method is generally unchanged.
Tab control (English name: TabControl), this control is used in the development of some more complex and user-interactive software (such as game assistance). There are too many things to set, and one interface is definitely not enough, and it is not convenient to manage.
The specific implementation steps are as follows:
1. Create a basic dialog program first, drag the TabControl control into it, and adjust the size. as follows
Figure (Figure 1):
Then add a variable whose category is Control for the control (the associated variable here is named m_TabCtrlMain). As shown below (Figure 2):
After compiling, run it to see the effect. The tab at this time is a blank page. As shown below (Figure 3):
2. Open the dialog cpp file, and add options for the tab control in the initialization function (here to add "Daguai" and "
The specific operation is as follows (Figure 4):
Now compile and run again. At this time, there are already options ("Daguai" and "Protect") in the tab control interface. The effect is as follows (Figure 5):
3. Next, we associate the corresponding interface windows for the "Demonstration" and "Protect" options.
Operation method: Right-click the project in the resource manager, and add two window resources to the project. In order to facilitate the distinction, we change the ID name of one of the windows to IDD_DaGuai, and the ID name of the other window to IDD_BaoHu. As shown below (Figure 6, Figure 7):
These two windows will eventually be displayed in the tab, so the border is not required, and the property-appearance-border is set to None.
As shown below (Figure 8):
4. Then add classes for these two windows (the corresponding class names are CPageBaoHu and CPageDaGuai), as shown in the following figure (Figure 9):
After adding the class, the corresponding header file and source file will be automatically generated, and then include these two at the beginning of the dialog header file
head File. As shown below (Figure 10):
5. Add the member variables of these two classes in the dialog header file, as shown in the following figure (Figure 11):
Then write the code to create the window in the initialization function of the cpp file, the code is as follows:
m_PageBaoHu.Create(CPageBaoHu::IDD); m_PageDaGuai.Create(CPageDaGuai::IDD);
6. We also need to write a click event handler for the tab control, so that the corresponding window is displayed after clicking the tab
,code show as below:
void CTabControlDlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: add control notification handler code here switch(m_TabCtrlMain.GetCurSel()) { case 0: m_PageBaoHu.ShowWindow(SW_SHOW); m_PageDaGuai.ShowWindow(SW_HIDE); break; case 1: m_PageDaGuai.ShowWindow(SW_SHOW); m_PageBaoHu.ShowWindow(SW_HIDE); break; } *pResult = 0; }
In order to distinguish the two windows when they are displayed, we also need to make a mark on the two windows (just add the upper and lower positions of the window.
Add a static text). Then compile and run the program, as shown in the following figure (Figure 12):
7. From Figure 12, we found a problem: the displayed window is outside the tab, which is not the effect we want at all. To solve this problem, we need to specify a parent window for the created window. The code is as follows:
m_PageBaoHu.SetParent(&m_TabCtrlMain); m_PageDaGuai.SetParent(&m_TabCtrlMain);
The running effect after compilation is as follows (Figure 13):
Now the window is displayed in the tab, but the edges are connected with gaps. To solve this problem, we need to
Get the window area size of the parent window. The complete code is as follows:
// TODO: add extra initialization code here RECT tabRect; m_TabCtrlMain.GetClientRect(&tabRect);//Get the coordinates of the size of the tab area (up, down, left and right) tabRect.top+=26;//Make some tweaks to the coordinates, this 26 is the height of the tab button tabRect.left+=2;//This is the width of the border m_TabCtrlMain.InsertItem(0,_T("Daguai")); m_TabCtrlMain.InsertItem(0,_T("Protect")); m_PageBaoHu.Create(CPageBaoHu::IDD);//create window m_PageDaGuai.Create(CPageDaGuai::IDD); m_PageBaoHu.SetParent(&m_TabCtrlMain);//Set a parent window for the created window m_PageDaGuai.SetParent(&m_TabCtrlMain); m_PageBaoHu.MoveWindow(&tabRect);//Move the window to the specified area m_PageDaGuai.MoveWindow(&tabRect);
The running effect after compilation is as follows (Figure 14):
OK, perfect solution.