Tab control控件使用方法

在主对话框中初始化函数OnInitDialog()中加入 
 

// TODO: 在此添加额外的初始化代码
	TCITEM item;

	item.mask = TCIF_TEXT;

	item.pszText = _T("覆盖率数据收集");

	m_ctl_tb.InsertItem (0,&item);

	item.pszText = _T("覆盖率报告处理");

	m_ctl_tb.InsertItem (1,&item);

	item.pszText = _T("覆盖率数据导出");

	m_ctl_tb.InsertItem (2,&item);

	m_dlg1.Create (IDD_DIALOG1,&m_ctl_tb);

	m_dlg2.Create (IDD_DIALOG2,&m_ctl_tb);

	m_dlg3.Create(IDD_DIALOG3,&m_ctl_tb);

	m_dlg1.SetWindowPos (NULL,10,30,400,100,SWP_SHOWWINDOW);

	m_dlg2.SetWindowPos (NULL,10,30,400,100,SWP_HIDEWINDOW );

	m_dlg3.SetWindowPos (NULL,10,30,400,100,SWP_HIDEWINDOW );

this->m_ctl_tb.InsertItem(0,_T("覆盖率数据收集"));
	this->m_ctl_tb.InsertItem(1,_T("覆盖率报告处理"));
	this->m_ctl_tb.InsertItem(2,_T("覆盖率数据导出"));

	//关联对话框,并且将IDC_TABTEST控件设为父窗口 
	m_dlg1.Create(IDD_DIALOG1,&m_ctl_tb); 
	m_dlg2.Create(IDD_DIALOG2,&m_ctl_tb); 
	m_dlg3.Create(IDD_DIALOG3,&m_ctl_tb);

	//获得IDC_TABTEST客户区大小 
	CRect rs; 
	m_ctl_tb.GetClientRect(&rs); 
	//调整子对话框在父窗口中的位置 
	rs.top+=20;  
	rs.bottom-=3;  
	rs.left+=1;  
	rs.right-=3;  

	//设置子对话框尺寸并移动到指定位置 
	m_dlg1.MoveWindow(&rs); 
	m_dlg2.MoveWindow(&rs); 
	m_dlg3.MoveWindow(&rs); 

	//分别设置隐藏和显示 
	m_dlg1.ShowWindow(true); 
	m_dlg2.ShowWindow(false); 
	m_dlg3.ShowWindow(false); 

	//设置默认的选项卡 
	m_ctl_tb.SetCurSel(0); 

然后加入消息处理函数:

void CNCSReport6Dlg::OnSelchangeTab1(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	CRect r;

	m_ctl_tb.GetClientRect (&r);

	switch(m_ctl_tb.GetCurSel())

	{

	case 0:

		m_dlg1.SetWindowPos (NULL,10,30,r.right -20,r.bottom -40,SWP_SHOWWINDOW);

		m_dlg2.SetWindowPos (NULL,10,30,r.right -20,r.bottom -40,SWP_HIDEWINDOW );  

		m_dlg3.SetWindowPos (NULL,10,30,r.right -20,r.bottom -40,SWP_HIDEWINDOW );

		break;

	case 1:

		m_dlg1.SetWindowPos (NULL,10,30,r.right -20,r.bottom -40,SWP_HIDEWINDOW);

		m_dlg2.SetWindowPos (NULL,10,30,r.right -20,r.bottom -40,SWP_SHOWWINDOW );  

		m_dlg3.SetWindowPos (NULL,10,30,r.right -20,r.bottom -40,SWP_HIDEWINDOW );

		break;

	case 2:

		m_dlg1.SetWindowPos (NULL,10,30,r.right -20,r.bottom -40,SWP_HIDEWINDOW);

		m_dlg2.SetWindowPos (NULL,10,30,r.right -20,r.bottom -40,SWP_HIDEWINDOW );  

		m_dlg3.SetWindowPos (NULL,10,30,r.right -20,r.bottom -40,SWP_SHOWWINDOW );

		break;

	}

	*pResult = 0;
}


在CEdit中绘制Tab控件:

void CTabControlView::OnInitialUpdate()
{
	// There is only one view ever, so it only needs to do the initial
	// update once--otherwise the application is  resized needlessly.
	static BOOL bUpdatedOnce = FALSE;
	if (bUpdatedOnce)
		return;
	bUpdatedOnce = TRUE;

	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();

	if (m_wndTab.GetSafeHwnd () != NULL)
	{
		return;
	}

	CRect rectTab;

	m_wndTabLoc.GetWindowRect (&rectTab);
	ScreenToClient (&rectTab);

	m_wndTab.Create (CMFCTabCtrl::STYLE_3D, rectTab, this, 1,
		CMFCTabCtrl::LOCATION_TOP);

	m_wndTab.SetImageList (IDB_ICONS, 16, RGB (255, 0, 255));
	
	m_wnd1.Create (WS_CHILD | WS_VISIBLE, CRect (0, 0, 0, 0), &m_wndTab, 1);
	m_wnd1.SetFont (&afxGlobalData.fontRegular);
	m_wnd1.SetWindowText (_T("Edit 1"));

	m_wnd2.Create (WS_CHILD | WS_VISIBLE, CRect (0, 0, 0, 0), &m_wndTab, 2);
	m_wnd2.SetFont (&afxGlobalData.fontRegular);
	m_wnd2.SetWindowText (_T("Edit 2"));

	m_wnd3.Create (WS_CHILD | WS_VISIBLE, CRect (0, 0, 0, 0), &m_wndTab, 3);
	m_wnd3.SetFont (&afxGlobalData.fontRegular);
	m_wnd3.SetWindowText (_T("Edit 3"));

	m_wnd4.Create (WS_CHILD | WS_VISIBLE, CRect (0, 0, 0, 0), &m_wndTab, 4);
	m_wnd4.SetFont (&afxGlobalData.fontRegular);
	m_wnd4.SetWindowText (_T("Edit 4"));

	m_wndTab.AddTab (&m_wnd1, _T("One"), 0, FALSE);
	m_wndTab.AddTab (&m_wnd2, _T("Two"), 1, FALSE);
	m_wndTab.AddTab (&m_wnd3, _T("Three"), 2, FALSE);
	m_wndTab.AddTab (&m_wnd4, _T("Four"), 3, FALSE);

	m_nTabStyle = theApp.GetInt (_T("TabStyle"), 0);
	m_nTabLocation = theApp.GetInt (_T("TabLocation"), 0);
	m_nColor = theApp.GetInt (_T("TabColor"), 0);
	m_bTabIcons = theApp.GetInt (_T("TabIcons"), TRUE);
	m_bTabsSwap = theApp.GetInt (_T("TabsSwap"), TRUE);

	UpdateData (FALSE);

	OnSelchangeTabStyle();
	OnLocation();
	OnColor();
	OnTabIcons ();
	OnTabSwap ();
}

其中标签选中改变样式响应函数:

void CTabControlView::OnSelchangeTabStyle() 
{
	UpdateData ();

	CMFCTabCtrl::Style style;
	int nTab = 0;

	m_wndTabIcons.EnableWindow ();

	for (nTab = 0; nTab < m_wndTab.GetTabsNum (); nTab++)
	{
		m_wndTab.SetTabIcon (nTab, m_bTabIcons ? nTab : (UINT)-1);
	}

	switch (m_nTabStyle)
	{
	case 1:
		style = CMFCTabCtrl::STYLE_FLAT;
		m_wndTabIcons.EnableWindow (FALSE);

		for (nTab = 0; nTab < m_wndTab.GetTabsNum (); nTab++)
		{
			m_wndTab.SetTabIcon (nTab, (UINT)-1);
		}

		break;

	case 2:
		style = CMFCTabCtrl::STYLE_3D_ONENOTE;
		break;

	case 3:
		style = CMFCTabCtrl::STYLE_3D_VS2005;
		break;

	case 4:
		style = CMFCTabCtrl::STYLE_3D_ROUNDED;
		break;
	default:
		style = CMFCTabCtrl::STYLE_3D;
	}

	m_wndTab.ModifyTabStyle (style);
	m_wndTab.RecalcLayout ();
	m_wndTab.RedrawWindow ();

	theApp.WriteInt (_T("TabStyle"), m_nTabStyle);
}

位置改变响应函数:

void CTabControlView::OnLocation() 
{
	UpdateData ();

	CMFCTabCtrl::Location location;

	switch (m_nTabLocation)
	{
	case 1:
		location = CMFCTabCtrl::LOCATION_BOTTOM;
		break;

	default:
		location = CMFCTabCtrl::LOCATION_TOP;
	}

	m_wndTab.SetLocation (location);
	m_wndTab.RecalcLayout ();
	m_wndTab.RedrawWindow ();

	theApp.WriteInt (_T("TabLocation"), m_nTabLocation);
}

颜色改变响应函数:

void CTabControlView::OnColor() 
{
	UpdateData ();
	
	CArray<COLORREF, COLORREF> arColors;

	switch (m_nColor)
	{
	case 0:
		m_wndTab.EnableAutoColor (FALSE);
		break;

	case 1:
		m_wndTab.EnableAutoColor (TRUE);
		break;

	case 2:
		arColors.Add (RGB (121, 210, 231));
		arColors.Add (RGB (190, 218, 153));
		arColors.Add (RGB (255, 170, 100));

		m_wndTab.EnableAutoColor (TRUE);
	}

	m_wndTab.SetAutoColors (arColors);

	theApp.WriteInt (_T("TabColor"), m_nColor);
}

标签图标改响应函数:

void CTabControlView::OnTabIcons() 
{
	UpdateData ();

	for (int nTab = 0; nTab < m_wndTab.GetTabsNum (); nTab++)
	{
		m_wndTab.SetTabIcon (nTab, m_bTabIcons ? nTab : (UINT)-1);
	}

	m_wndTab.RecalcLayout ();
	m_wndTab.RedrawWindow ();

	theApp.WriteInt (_T("TabIcons"), m_bTabIcons);
}

其它响应函数:

void CTabControlView::AddNotification (LPCTSTR lpszText)
{
	if (m_wndNotifications.GetCount () > 100)
	{
		m_wndNotifications.DeleteString (0);
	}

	int nIndex = m_wndNotifications.AddString (lpszText);
	m_wndNotifications.SetCurSel (nIndex);
}

LRESULT CTabControlView::OnMoveTab(WPARAM wp, LPARAM lp)
{
	CString str;
	str.Format (_T("AFX_WM_ON_MOVE_TAB: %d %d"), (int) wp, (int) lp);

	AddNotification (str);
	return 0;
}

LRESULT CTabControlView::OnChangeActiveTab(WPARAM wp, LPARAM /*lp*/)
{
	CString str;
	str.Format (_T("AFX_WM_CHANGE_ACTIVE_TAB: %d"), (int) wp);

	AddNotification (str);
	return 0;
}

LRESULT CTabControlView::OnChangingActiveTab(WPARAM wp, LPARAM /*lp*/)
{
	CString str;
	str.Format (_T("AFX_WM_CHANGING_ACTIVE_TAB: %d"), (int) wp);

	AddNotification (str);
	return 0;
}

void CTabControlView::OnTabSwap() 
{
	UpdateData ();

	m_wndTab.EnableTabSwap (m_bTabsSwap);
	theApp.WriteInt (_T("TabsSwap"), m_bTabsSwap);
}


你可能感兴趣的:(Tab control控件使用方法)