向对话框中拖入一个tab control控件,调整好大小以适合要求
对于控件类,我们需要定义一个控件类的对象,通过对象的成员方法来将需要添加的内容显示在对话框中,并通过一定的逻辑来控制标签项的显示顺序。
创建一个新的dialog,并且在属性中进行如下设置:
在头文件中添加
CTabCtrlm_Tab;
CVMC_Packagem_Package;
CVMC_NandAccessm_NandAccess;
进入要添加控件的对话框
首先需要在如下函数中添加linked代码,否则运行时报错。
voidCVmc_Dld::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//linked
DDX_Control(pDX, IDC_TAB_DLD, m_tab);
}
添加如下代码完成标签的显示,
CRect tabRect;//size and position of tabCtrl in client area
m_Tab.InsertItem(0, _T("Package"));//Inser PackageDialog into Item
m_Tab.InsertItem(1, _T("NAND"));//Insert NANDDialog into Item
//link dialog to tab control
m_Package.Create(IDD_DIALOG_PACKAGE, &m_Tab);
m_NandAccess.Create(IDD_DIALOG_NAND, &m_Tab);
m_Tab.GetClientRect(&tabRect);//get client rect
//modify tabRect, adapt to main dialog
tabRect.left += 1;
tabRect.right -= 1;
tabRect.bottom -= 1;
tabRect.top += 20;
//set Packge display default
m_Package.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);
m_NandAccess.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
这样我们只是设置了再默认情况下的标签显示,但是当真正在程序运行时,便签内的内容并不能够正常显示,因此需要对事件的处理事件编程。
在此处选择当选择项发生改变时的消息处理,
当然也可以手动的编写代码完成,需要编辑
BEGIN_MESSAGE_MAP(CVMC_MainDialog,CDialogEx)
ON_NOTIFY(TCN_SELCHANGE, IDC_TAB_ALLDIALOG,&CVMC_MainDialog::OnTcnSelchangeTabAlldialog)
END_MESSAGE_MAP()
并添加
voidCVMC_MainDialog::OnTcnSelchangeTabAlldialog(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
函数的声明,现在我们就需要编辑上面的方法完成我们需要的动作。具体代码的编写如下:
//getcurrent selected tab and change it to show
switch(m_Tab.GetCurSel())
{
//change the show window as you want
case0:
//set Package show
m_Package.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);
//set others hide
m_NandAccess.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
m_SpiNorFlash.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
break;
case1:
//set NAND Flash show
m_NandAccess.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);
//set others hide
m_Package.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
m_SpiNorFlash.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
break;
case2:
//set SPI-NOR Flash show
m_SpiNorFlash.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_SHOWWINDOW);
//set others hide
m_Package.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
m_NandAccess.SetWindowPos(NULL, tabRect.left, tabRect.top,tabRect.Width(), tabRect.Height(), SWP_HIDEWINDOW);
break;
default:
break;
}