formitemgroup.cpp

来自「基于WINDOWS mobile 的用于创建一个窗体和自定义试图的工程」· C++ 代码 · 共 267 行

CPP
267
字号
// FormItemGroup.cpp: implementation of the CFormItemGroup class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FormItemGroup.h"
#include "DrawTextEx.h"
#include "FormListCtrl.h"

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

using namespace DRA;


//---------------------------------------------------------------------------
//
//	CFormItemGroup constructors and destructor
//
//---------------------------------------------------------------------------


CFormItemGroup::CFormItemGroup()
:	m_bExpanded	(TRUE)
{
	m_vecItem.reserve(16);
}


CFormItemGroup::~CFormItemGroup()
{
	// The items do not belong to the group so they are not destroyed here.
}


//---------------------------------------------------------------------------
//
//	CFormItemGroup operations
//
//---------------------------------------------------------------------------


// CFormItemGroup::PaintButton
//
//		Paints the expand / collapse button
//
void CFormItemGroup::PaintButton(HDC hDC, RECT &rc)
{
	HPEN		hBoxPen,
				hMrkPen,
				hOldPen;
	HBRUSH		hNewBrush,
				hOldBrush;
	
	int			h	= rc.bottom - rc.top,
				x	= rc.left + SCALEX(4),//(m_nIndent - 9) / 2,
				y	= rc.top + (h - SCALEY(9)) / 2 + SCALEY(1);

	hBoxPen		= ::CreatePen(PS_SOLID, SCALEX(1), RGB(192, 192, 192));
	hMrkPen		= ::CreatePen(PS_SOLID, SCALEX(1), RGB(  0,   0,   0));
	hNewBrush	= ::CreateSolidBrush(RGB(255, 255, 255));

	hOldPen		= (HPEN)	::SelectObject(hDC, hBoxPen);
	hOldBrush	= (HBRUSH)	::SelectObject(hDC, hNewBrush);

	//
	// Draw the box
	//
	DRA::Rectangle(hDC, x, y, x + SCALEX(9), y + SCALEY(9));

	//
	// Now, the - or + sign
	//
	::SelectObject(hDC, hMrkPen);

	LineHorz(hDC, x + SCALEX(2), x + SCALEY(7), y + SCALEY(4));			// '-'

	if(!m_bExpanded)
		LineVert(hDC, x + SCALEX(4), y + SCALEY(2), y + SCALEY(7));		// '+'

	::SelectObject(hDC, hOldPen);
	::SelectObject(hDC, hOldBrush);

	::DeleteObject(hMrkPen);
	::DeleteObject(hBoxPen);
	::DeleteObject(hNewBrush);
}


// CFormItemGroup::InsertChildren
//
//		Inserts the group's children at the given index
//
int CFormItemGroup::InsertChildren(CFormListCtrl *pForm, int iItem)
{
	size_t	i;
	int		iIndex;

	ASSERT(m_bExpanded == TRUE);

	for(i = 0; i < m_vecItem.size(); ++i)
	{
		if(m_vecItem[i]->IsVisible())
		{
			iIndex = m_vecItem[i]->InsertOn(pForm, iItem);
			++iItem;
		}
	}

	return iIndex;
}


// CFormItemGroup::DeleteChildren
//
//		Deletes all the children at a given index
//
void CFormItemGroup::DeleteChildren(CFormListCtrl *pForm, int iIndex)
{
	size_t	i;

	ASSERT(m_bExpanded == FALSE);

	for(i = 0; i < m_vecItem.size(); ++i)
		if(m_vecItem[i]->IsVisible())
			pForm->DeleteItem(iIndex);
}


//---------------------------------------------------------------------------
//
//	CFormItemGroup virtual methods
//
//---------------------------------------------------------------------------


// CFormItemGroup::InsertOn
//
//		Inserts the group on the form
//
int CFormItemGroup::InsertOn(CFormListCtrl* pForm, int iItem)
{
	int		iIndex;

	iIndex = CFormItem::InsertOn(pForm, iItem);
	
	if(!m_bExpanded)
		return iIndex;

	if(iIndex != -1)
		InsertChildren(pForm, iIndex + 1);

	return iIndex;
}


// CFormItemGroup::ShowEditor
//
//		The user clicked the group header
//
BOOL CFormItemGroup::ShowEditor(CFormListCtrl* pForm, BOOL bShow, int iItem, int iSubItem)
{
	//
	// Check if the item is being activated
	//
	if(bShow)
	{
		m_bExpanded = !m_bExpanded;

		if(m_bExpanded)
			InsertChildren(pForm, iItem + 1);
		else
			DeleteChildren(pForm, iItem + 1);
		
		pForm->RedrawItems(iItem, iItem);
		pForm->SetEditIndex(-1);
	}
	
	return TRUE;
}


// CFormItemGroup::CustomDraw
//
//		Draws the group header
//
LRESULT CFormItemGroup::CustomDraw(CFormListCtrl* pForm, NMLVCUSTOMDRAW *pLVCD)
{
	DWORD	dwDrawStage = pLVCD->nmcd.dwDrawStage;
	LRESULT	lRet		= CDRF_SKIPDEFAULT;
	HDC		hDC			= pLVCD->nmcd.hdc;
	RECT	rc			= pLVCD->nmcd.rc,
			rcItem;

	pForm->GetClientRect(&rcItem);

	rcItem.top		= rc.top;
	rcItem.bottom	= rc.bottom;
	rcItem.right	-= GetSystemMetrics(SM_CXVSCROLL);
	rcItem.left		+= DRA::SCALEX(20);

	if(dwDrawStage == (CDDS_ITEMPREPAINT | CDDS_SUBITEM))
	{
		HPEN	hPenNew,
				hPenOld;

		hPenNew = ::CreatePen(PS_SOLID, 1, RGB(128, 128, 128));

		if(pLVCD->iSubItem == 0)
		{
			HFONT		hFontOld;
			COLORREF	crText;

			PaintButton(hDC, rc);

			hFontOld = (HFONT)SelectObject(hDC, *pForm->GetBoldFont());
			crText = ::SetTextColor(hDC, RGB(0, 0, 156));

			DrawText(hDC, m_strCaption, -1, &rcItem, DT_LEFT | DT_VCENTER | DT_SINGLELINE);

			SetTextColor(hDC, crText);
			SelectObject(hDC, hFontOld);

			hPenOld = (HPEN)::SelectObject(hDC, hPenNew);
			LineHorz(hDC, rcItem.left, rc.right, rc.bottom - 1);
			::SelectObject(hDC, hPenOld);
		}
		else
		{
			hPenOld = (HPEN)::SelectObject(hDC, hPenNew);
			LineHorz(hDC, rc.left, rc.right, rc.bottom - 1);
			::SelectObject(hDC, hPenOld);
		}

		::DeleteObject(hPenNew);

		rcItem.left -= SCALEX(4);
		DrawFocus(hDC, &rcItem);

		lRet = CDRF_SKIPDEFAULT;
	}
	else if(dwDrawStage == CDDS_ITEMPOSTPAINT)
	{
	}

	return lRet;
}


// CFormItemGroup::ValidateData
//
//		Validates the group data
//
BOOL CFormItemGroup::ValidateData()
{
	size_t	i;
	BOOL	bValid	= TRUE;

	for(i = 0; bValid && (i < m_vecItem.size()); ++i)
		bValid = bValid && m_vecItem[i]->ValidateData();

	return bValid;
}

⌨️ 快捷键说明

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