Open VS2019 and create an MFC
application based on the dialog box MFCSample
. Removes the default generated controls for the dialog.
After observation, it can be seen that there are three categories in the default generated project:
CAboutDialog
CMFCSampleApp
CMFCSampleDlg
The main dialog class CMFCSampleDlg
inherits from CDialogEx
, and in addition to the standard default constructor CMFCSampleDlg(CWnd* pParent = nullptr)
, 5 functions exist:
DoDataExchange(CDataExchange* pDX)
OnInitDialog()
OnPaint()
OnQueryDragIcon()
OnSysCommand(UINT nID, LPARAM lParam)
The App
class CMFCSampleApp
instantiates a unique object theApp
, and the program initialization function InitInstance()
serves as the entry point of the program.
Drag a Group Box
control into the toolbox and change the text to: parameter .
Then drag in six static text controls, associate variables with each, and place them into the Group Box
controls.
Drag in six button controls, associate variables with each, and lay them out Group Box
below the controls.
The right side of the interface is to be placed Tab Control
with controls, and each tab is dialog-based. Reference article: https://www.cnblogs.com/tinaluo/p/9675586.html
After running, Tab Control
nothing appears in the control because no content has been added yet.
So, insert two dialogs first, add dialog-based classes for the two dialogs: MyTabDlg1
and MyTabDlg2
, now the question is how to insert the two dialogs into the above tabs.
Remember to modify two properties of the dialog
First, in the main window class CMFCSampleDlg
, first Tab Control
associate the control with the variablem_Tab
Then manually add the instantiation variables of the two dialog boxes in this class,NoticeAdd the header file.
In addition, for subsequent Tab
tab switching, two variables need to be added to the class
int m_CurSelTab;
CDialog* pDialog[5]; //用来保存对话框对象指针
Next, you can use m_Tab
variables to add two dialog variables dlg1
and to the dlg2
tab.
CMFCSampleDlg::OnInitDialog()
Add initialization code to the function
// 添加选项卡
m_Tab.InsertItem(0, _T("标签页1"));
m_Tab.InsertItem(1, _T("标签页2"));
m_Tab.InsertItem(2, _T("标签页3"));
m_Tab.InsertItem(3, _T("标签页4"));
m_Tab.InsertItem(4, _T("标签页5"));
//创建两个对话框
dlg1.Create(IDD_DIALOG1, &m_Tab);
dlg2.Create(IDD_DIALOG2, &m_Tab);
//设定在Tab内显示的范围
CRect rc;
m_Tab.GetClientRect(rc);
rc.top += 30;
rc.bottom -= 0;
rc.left += 0;
rc.right -= 0;
dlg1.MoveWindow(&rc);
dlg2.MoveWindow(&rc);
//把对话框对象指针保存起来
pDialog[0] = &dlg1;
pDialog[1] = &dlg2;
//显示初始页面
pDialog[0]->ShowWindow(SW_SHOW);
pDialog[1]->ShowWindow(SW_HIDE);
//保存当前选择
m_CurSelTab = 0;
And add Tab Control
a message handler for control switching
void CMFCSampleDlg::OnTcnSelchangeTab2(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: 在此添加控件通知处理程序代码
//把当前的页面隐藏起来
pDialog[m_CurSelTab]->ShowWindow(SW_HIDE);
//得到新的页面索引
m_CurSelTab = m_Tab.GetCurSel();
//把新的页面显示出来
pDialog[m_CurSelTab]->ShowWindow(SW_SHOW);
*pResult = 0;
}
final effect