Click here to Skip to main content
15,886,788 members
Articles / Desktop Programming / MFC

An MFC-CListCtrl derived class that allows other ‘controls’ to be inserted into a particular cell

Rate me:
Please Sign up or sign in to vote.
4.92/5 (54 votes)
5 Jan 2014CPOL12 min read 167.6K   14K   160  
A class derived from CListCtrl that allows edit controls, combo boxes, check boxes, date pickers, and color pickers to be inserted into or removed from particular cells extremely easily. The inserted 'controls' are not CWnd-derived.
#include "stdafx.h"
#include "CellColorCtrl.h"
#include "PopUpColorBar.h"

DWORD CCellColorCtrl::m_dwDateCtrlStyle = 0;

CCellColorCtrl::CCellColorCtrl() //: m_Color(0x00ABCDEF)
{
	m_pPopUpWnd = new CPopupColorBar;
}

CCellColorCtrl::~CCellColorCtrl()
{
	delete m_pPopUpWnd;
}

void CCellColorCtrl::DrawCtrl(CDC *pDC, const LPRECT prcCell, UINT uiItemState /*= 0*/)
{
	CBrush			Brush;
	int				iSavedDC;
	RECT			rcColorArea;

	Brush.CreateSolidBrush(GetSysColor(IsDisabled()? COLOR_BTNFACE : COLOR_WINDOW));
	pDC->FillRect(prcCell, &Brush);
	Brush.DeleteObject();

	iSavedDC = pDC->SaveDC();
	BOOL bThemeDrawn = DrawThemeCombobox(pDC->m_hDC, prcCell);

	COLORREF SelColor = GetSelectedColor();

	Brush.CreateSolidBrush(SelColor);

	CopyRect(&rcColorArea, prcCell); 
	
	InflateRect(&rcColorArea, -::GetSystemMetrics(SM_CXFIXEDFRAME), -::GetSystemMetrics(SM_CYFIXEDFRAME));
	rcColorArea.right -= ::GetSystemMetrics(SM_CXVSCROLL);
	
	if (rcColorArea.right > rcColorArea.left)
		pDC->FillRect(&rcColorArea, &Brush);

	Brush.DeleteObject();

	if (!bThemeDrawn)
	{
		pDC->DrawEdge(prcCell, EDGE_SUNKEN, BF_RECT);
		CCellDropDown::DrawCtrl(pDC, prcCell, uiItemState);
	}
	pDC->RestoreDC(iSavedDC);
}

void CCellColorCtrl::Initialize(HWND hParentWnd, BOOL *pParentEnabled, LPCTSTR strText)
{
	CCellDropDown::Initialize(hParentWnd, pParentEnabled, strText);

	CPopupColorBar *p = dynamic_cast<CPopupColorBar *>(m_pPopUpWnd);

	if (p)
	{
		p->Create(m_hParentWnd, this);		
	}
}

int CCellColorCtrl::GetMinWidth()
{
	return CCellDropDown::GetMinWidth();
}

void CCellColorCtrl::AddToCtrlTypeStyle(DWORD dwFlags)
{
}

void CCellColorCtrl::RemoveFromCtrlTypeStyle(DWORD dwFlags)
{
}

LONG CCellColorCtrl::GetStyle()
{
	return m_dwDateCtrlStyle | m_dwStyle;
}

void CCellColorCtrl::OnOpenDropDown()
{
	CPopupColorBar *pPopUpColorBar = dynamic_cast<CPopupColorBar *>(m_pPopUpWnd);

	if (pPopUpColorBar)
		pPopUpColorBar->OpenDropDown(GetSelectedColor());
}

void CCellColorCtrl::GetDropDownBoundingRect(RECT &rc, BOOL downwards /*= TRUE*/)
{
	CPopupColorBar *p = dynamic_cast<CPopupColorBar *>(m_pPopUpWnd);

	if (p)
	{
		p->GetBoundingRect(m_rcBounding, rc, downwards);
	}
}

void CCellColorCtrl::OnSelCloseDropDown(LPARAM lParam)
{
	m_strText.Format(_T("%#010X"), lParam); 
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions