MFC program example

Open VS2019 and create an MFCapplication based on the dialog box MFCSample. Removes the default generated controls for the dialog.

insert image description here
insert image description here
After observation, it can be seen that there are three categories in the default generated project:

  1. CAboutDialog
  2. CMFCSampleApp
  3. CMFCSampleDlg

The main dialog class CMFCSampleDlginherits 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 Appclass CMFCSampleAppinstantiates a unique object theApp, and the program initialization function InitInstance()serves as the entry point of the program.

Drag a Group Boxcontrol 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 Boxcontrols.

Drag in six button controls, associate variables with each, and lay them out Group Boxbelow the controls.

insert image description here
The right side of the interface is to be placed Tab Controlwith controls, and each tab is dialog-based. Reference article: https://www.cnblogs.com/tinaluo/p/9675586.html

insert image description here
After running, Tab Controlnothing appears in the control because no content has been added yet.

insert image description here

So, insert two dialogs first, add dialog-based classes for the two dialogs: MyTabDlg1and MyTabDlg2, now the question is how to insert the two dialogs into the above tabs.
insert image description here
Remember to modify two properties of the dialog

insert image description here

First, in the main window class CMFCSampleDlg, first Tab Controlassociate the control with the variablem_Tab

insert image description here
Then manually add the instantiation variables of the two dialog boxes in this class,NoticeAdd the header file.

insert image description here
In addition, for subsequent Tabtab switching, two variables need to be added to the class

int m_CurSelTab;
CDialog* pDialog[5];  //用来保存对话框对象指针

Next, you can use m_Tabvariables to add two dialog variables dlg1and to the dlg2tab.

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 Controla 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

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/Star_ID/article/details/126558358