1. 新建一个MFC工程,取名xyTabControl,选择Dialog based,然后Finish。
2. 删除对话框上默认添加的三个控件。添加Tab Control控件并在Property属性中设置ID为IDC_TAB1,添加变量m_tabctrl,类型为CTabCtrl。
3. 在对话框的初始化函数OnInitDialog里面添加如下代码:
<wbr><wbr>m_tabctrl.InsertItem(0,"memo0");<wbr><wbr><wbr><wbr><wbr><wbr>//添加参数一选项卡<wbr><br><wbr><wbr>m_tabctrl.InsertItem(1,"memo1");<wbr><wbr><wbr><wbr><wbr>//添加参数二选项卡</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
4.在对话框资源里面添加两个对话框资源, ID分别命名为IDD_PARA1, IDD_PARA2。字体为宋体, 字号为9, style为Child, Border为None, 调整高度宽度到适中尺寸。再分别为其添加对应的基于CDialog类CPara1, CPara2。
5. 在CxyTabControlDlg类中添加两个成员变量m_para1, m_para2, 分别是两个子对话框的实例. 代码如下:
CPara2 m_para2;
CPara1 m_para1;
6. 布置IDD_PARA1和IDD_PARA2对话框 如下图:
7. 在IDD_xyTabControl_DIALOG对话框的初始化函数OnInitDialog里面添加如下代码:
//关联对话框,并且将IDC_TABTEST控件设为父窗口
<wbr><wbr><wbr>m_para1.Create(IDD_PARA1,GetDlgItem(IDC_TAB1));<br><wbr><wbr><wbr>m_para2.Create(IDD_PARA2,GetDlgItem(IDC_TAB1));</wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>//获得IDC_TABTEST客户区大小</wbr></wbr></wbr>
<wbr><wbr><wbr>CRect rs;<br><wbr><wbr><wbr><wbr>m_tabctrl.GetClientRect(&rs);<br><wbr><wbr><wbr>//调整子对话框在父窗口中的位置<br><wbr><wbr><wbr>rs.top += 20;<br><wbr><wbr><wbr>rs.bottom -= 20;<br><wbr><wbr><wbr>rs.left += 1;<br><wbr><wbr><wbr>rs.right -= 2;<br><wbr><wbr><wbr>//设置子对话框尺寸并移动到指定位置<br><wbr><wbr><wbr>m_para1.MoveWindow(&rs);<br><wbr><wbr><wbr>m_para2.MoveWindow(&rs);<br><wbr><wbr><wbr>//分别设置隐藏和显示<br><wbr><wbr><wbr>m_para1.ShowWindow(1);<br><wbr><wbr><wbr>m_para2.ShowWindow(0);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>//设置默认的选项卡<br><wbr><wbr><wbr>m_tabctrl.SetCurSel(0);</wbr></wbr></wbr></wbr></wbr></wbr>
8. 添加Tab Control控件的TCN_SELCHANGE事件响应函数OnSelchangeTabtest(NMHDR* pNMHDR, LRESULT* pResult) ,函数体代码如下:
<wbr><wbr><wbr>int CurSel = m_tabctrl.GetCurSel();<br><wbr><br><wbr><wbr><wbr>switch(CurSel)<br><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>case 0:<br><wbr><wbr><wbr><wbr><wbr><wbr>m_para1.ShowWindow(true);<br><wbr><wbr><wbr><wbr><wbr><wbr>m_para2.ShowWindow(false);<br><wbr><wbr><wbr><wbr>break;<br><wbr><wbr><wbr><wbr>case 1:<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>m_para1.ShowWindow(false);<br><wbr><wbr><wbr><wbr><wbr><wbr>m_para2.ShowWindow(true);<br><wbr><wbr><wbr><wbr>break;<br><wbr><wbr><wbr><wbr><wbr>default:<br><wbr><wbr><wbr><wbr>;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>*pResult = 0;<br><wbr><wbr><wbr>}<br>9.好了,完成,这个小程序很简单,效果如下图:</wbr></wbr></wbr></wbr></wbr></wbr></wbr>
参考推荐: