Use tab control and inherit your class (ctabsheet) from the ctabctrl control class.
First, I will introduce how to use ctabsheet.
You must first create a subdialog box class. This subdialog box class should not be inherited from cpropertypage, but directly from cdialog. In addition, the attributes of the sub-Dialog Box resources should be set to: style, border, and none.
In the main dialog box, add a tab control and adjust the position and size as appropriate. Use classwizard to create a ctabsheet control variable for this tab control.
STEP/Method
- Add:
m_sheet.addpage ("tab1", & m_page1, idd_dialog1);
m_sheet.addpage ("tab2", & m_page2, idd_dialog2);
m_sheet.show ();
in this way, you can create a perfect property page on the dialog box. The effect is exactly the same.
let's talk about the details of the ctabsheet class.
ctabsheet is inherited from ctabctrl and is used as the control class for tab control. There is a member variable in the class to record the pointer cdialog * m_ppages [maxpage] for each subdialog box; maxpage is the maximum value of the labels that can be loaded by the class.
the addpage method in the class is used to record the pointer of the subdialog box and the ID number of the resources in the used dialog box.
bool ctabsheet: addpage (lpctstr title, cdialog * pdialog, uint ID)
{< br> If (maxpage = m_nnumofpages)
return false;
// Save the total number of subdialogs
m_nnumofpages ++;
// record the sub-Dialog Box pointer, resource ID, text to be displayed on the label
m_ppages [m_nNumOfPages-1] = pdialog;
m_idd [m_nNumOfPages-1] = ID;
m_title [m_nNumOfPages-1] = title;
return true;
}
- After adding subdialogs to addpage, you must call the show method of ctabsheet to generate tags and subdialogs.
Void ctabsheet: Show ()
{
// Use cdialog: Create to create a subdialog box, and use ctabctrl: insertitem to add the corresponding tag
For (INT I = 0; I <m_nnumofpages; I ++)
{
M_ppages [I]-> Create (m_idd [I], this );
Insertitem (I, m_title [I]);
}
// Because the first label is selected by default when the dialog box is displayed, the first subdialog box should be displayed, and other subdialogs should be hidden.
M_ppages [0]-> showwindow (sw_show );
For (I = 1; I <m_nnumofpages; I ++)
M_ppages [I]-> showwindow (sw_hide );
Setrect ();
}
- After the tag and subdialog box are generated, call ctabsheet: setrect to calculate and adjust the size of the attribute page.
Void ctabsheet: setrect ()
{
Crect tabrect, itemrect;
Int NX, NY, nxc, NYC;
// Obtain the size of the tab control.
Getclientrect (& tabrect );
Getitemrect (0, & itemrect );
// Calculate the position and size of each subdialog box relative to tab control.
Nx = itemrect. Left;
NY = itemrect. Bottom + 1;
Nxc = tabrect. right-itemRect.left-2;
NYC = tabrect. bottom-nY-2;
// Adjust the subdialog box using the calculated data
M_ppages [0]-> setwindowpos (& wndtop, NX, NY, nxc, NYC, swp_showwindow );
For (INT ncount = 1; ncount <m_nnumofpages; ncount ++)
M_ppages [ncount]-> setwindowpos (& wndtop, NX, NY, nxc, NYC, swp_hidewindow );
}
After you click the tab bar, the corresponding subdialog box is displayed, and the displayed subdialog box is hidden. Therefore, classwizard is used to process wm_lbuttondown messages.
Void ctabsheet: onlbuttondown (uint nflags, cpoint point)
{
Ctabctrl: onlbuttondown (nflags, point );
// Determine whether other labels are clicked
If (m_ncurrentpage! = Getcurfocus ())
{
// Hide the original subdialog box
M_ppages [m_ncurrentpage]-> showwindow (sw_hide );
M_ncurrentpage = getcurfocus ();
// Display the subdialog box corresponding to the current tag
M_ppages [m_ncurrentpage]-> showwindow (sw_show );
}
}
In this way, the ctabsheet class can be used to easily place its own property pages in the dialog box, and controls are scattered in each subdialog box class, in line with the idea of object encapsulation. In addition, you can use classwizard to easily generate a message ing to process the tab control message. For example, you can process the tcn_selchange message to perform some actions when the tag is switched.