创建的子对话框必须设置style属性为child
1.添加标签页
CTabCtrl* ctbc = (CTabCtrl*)(GetDlgItem(IDC_TAB1));
ctbc->InsertItem(0, TEXT("chinese"));
ctbc->InsertItem(1, TEXT("english"));
2.创建对话框
m_dialog1.Create(IDD_DIALOG1, ctbc);
m_dialog2.Create(IDD_DIALOG2, ctbc);
3.获取标签控件客户区位置信息
CRect rect;
ctbc->GetClientRect(&rect);
rect.left += 1;
rect.right -= 1;
rect.top += 21;
rect.bottom -= 1;
4.设置对话框位置
m_dialog1.SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_SHOWWINDOW);
m_dialog2.SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_HIDEWINDOW);
5.消息映射
ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &CMFCTrainDlg::OnTcnSelchangeTab1)
6.消息处理
当选择不同的标签页时,标签控件不会自动切换,需要自己编写对话框切换代码
void CMFCTrainDlg::OnTcnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
CTabCtrl* ctbc = (CTabCtrl*)(GetDlgItem(IDC_TAB1));
CRect rect;
ctbc->GetClientRect(&rect);
rect.left += 1;
rect.right -= 1;
rect.top += 21;
rect.bottom -= 1;
switch (ctbc->GetCurSel())
{
case 0:
m_dialog1.SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_SHOWWINDOW);
m_dialog2.SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_HIDEWINDOW);
break;
case 1:
m_dialog1.SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_HIDEWINDOW);
m_dialog2.SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_SHOWWINDOW);
break;
default:
break;
}
}