⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabviewctrl.cpp

📁 Visual C++编写的工程解析器源代码
💻 CPP
字号:
// TabViewCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "TabViewCtrl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTabViewCtrl
IMPLEMENT_DYNCREATE(CTabViewCtrl, CTabCtrl)

CTabViewCtrl::CTabViewCtrl()
{
	m_nActivePage = -1;		//当前活动页
	CTabCtrl::CTabCtrl();
}

CTabViewCtrl::~CTabViewCtrl()
{
	CTabCtrl::~CTabCtrl();
}

BEGIN_MESSAGE_MAP(CTabViewCtrl, CTabCtrl)
	//{{AFX_MSG_MAP(CTabViewCtrl)
	ON_WM_SIZE()
	ON_WM_LBUTTONDOWN()
	ON_WM_NCPAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTabViewCtrl message handlers

BOOL CTabViewCtrl::Create(UINT wStyle, const CRect& rect, CWnd* pParentWnd, UINT nID)
{
	if(!CTabCtrl::Create(wStyle, rect, pParentWnd, nID))	return FALSE;
	//
	SetFont(CFont::FromHandle((HFONT)
		::GetStockObject(DEFAULT_GUI_FONT)));

	return TRUE;
}

int CTabViewCtrl::GetPageCount()
{
	return this->SendMessage(TCM_GETITEMCOUNT, 0, 0);
}

void CTabViewCtrl::AddPage(CWnd* pWnd, LPCTSTR lpszText, UINT uImageIndex)
{
	if( pWnd->m_hWnd && ::IsWindow(pWnd->m_hWnd) )
	{
		TCITEM tci;
		tci.mask = TCIF_TEXT | TCIF_IMAGE | TCIF_PARAM;
		
		tci.pszText = (LPTSTR)lpszText;
		tci.cchTextMax = MAX_TABCTRL_CAPTIONNAME_SIZE;	
		tci.iImage = uImageIndex;
		
		tci.lParam = (LPARAM)pWnd->m_hWnd;	//子窗体的句柄
		
		int nIndex = this->GetPageCount();
		this->InsertItem(nIndex, &tci);
		::SetParent(pWnd->m_hWnd, this->m_hWnd);
		
		this->SetActivePage(nIndex);
	}
}

void CTabViewCtrl::AddPage(CRuntimeClass* pClass,UINT nIDTemplate, LPCTSTR lpszText, UINT uImageIndex)
{
	CDialog *pDlg = (CDialog*)pClass->CreateObject();
	if(pDlg == NULL)	return;
	
	if(pDlg->Create(nIDTemplate, this))
	{
		AddPage(pDlg, lpszText, uImageIndex);
	}
}

BOOL CTabViewCtrl::RemovePage(int nIndex)
{
	if( nIndex >= 0 && nIndex < this->GetPageCount() )
	{		
		if(nIndex == m_nActivePage)		//如果为当前页
		{
			if(!this->DeleteItem(nIndex))	return FALSE;
			if(nIndex >= 0 && nIndex < this->GetPageCount())
				this->SetActivePage(nIndex);
			else if(nIndex == this->GetPageCount())
				this->SetActivePage(nIndex - 1);

			return TRUE;
		}		
	}
	return FALSE;
}

void CTabViewCtrl::RemoveAllPage()
{
	this->DeleteAllItems();
}

HWND CTabViewCtrl::GetPageWnd(int nIndex)
{
	TCITEM tci; 
	tci.mask = TCIF_PARAM;
	this->GetItem(nIndex, &tci);
	
	HWND hWnd = HWND(tci.lParam);
	return hWnd;
}

void CTabViewCtrl::SetActivePage(int nIndex)
{
	if( nIndex != m_nActivePage && nIndex >= 0 && nIndex < this->GetPageCount() )
	{
		HWND hWnd = this->GetPageWnd(m_nActivePage);
		::ShowWindow(hWnd, SW_HIDE);	//先隐藏掉当前活动窗口

		this->SetCurSel(nIndex);		//变换TAB
		
		hWnd = this->GetPageWnd(nIndex);
		ASSERT(hWnd != NULL);
		
		//伸缩子窗口
		CRect rect;
		this->GetClientRect(rect);
		::SetWindowPos(hWnd, NULL, rect.left, rect.top + MAX_TABCTRL_CAPTIONNAME_SIZE, rect.Width(), rect.Height(), SWP_SHOWWINDOW);

		m_nActivePage = nIndex;
	}
}

void CTabViewCtrl::OnSize(UINT nType, int cx, int cy) 
{
	CTabCtrl::OnSize(nType, cx, cy);
	
	if(this->GetSafeHwnd())
	{		
		HWND hWnd = this->GetPageWnd(m_nActivePage);
		if(hWnd != NULL)
		{			
			//伸缩子窗口
			CRect rect;
			this->GetClientRect(rect);
			::SetWindowPos(hWnd, NULL, rect.left, 
				rect.top + MAX_TABCTRL_CAPTIONNAME_SIZE, 
				rect.Width(), 
				rect.Height() - MAX_TABCTRL_CAPTIONNAME_SIZE, SWP_SHOWWINDOW);
		}
	}	
}

//
void CTabViewCtrl::OnNcPaint()
{
	HDC hdc = ::GetWindowDC(this->m_hWnd);
	
	CRect rect;
	GetClientRect(rect);
	rect.top += MAX_TABCTRL_CAPTIONNAME_SIZE;
		
	HPEN pen = ::CreatePen(PS_SOLID, 0, RGB(0, 0, 0));
	HGDIOBJ old = ::SelectObject(hdc, pen);
	DrawEdge(hdc, rect, EDGE_SUNKEN, BF_LEFT | BF_RIGHT | BF_TOP);
	
	::SelectObject(hdc, old);
	::DeleteObject(pen);
	::ReleaseDC(m_hWnd, hdc);
}
void CTabViewCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CTabCtrl::OnLButtonDown(nFlags, point);
	int nIndex = this->GetCurSel();
	SetActivePage(nIndex);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -