Click here to Skip to main content
15,885,278 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.4K   13.9K   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.
#pragma once

#include "CellDropDown.h"

#define DAY_CH				_T('d')
#define MONTH_CH			_T('M')
#define YEAR_CH				_T('y')
#define MIN_YEAR			1750
#define MAX_YEAR			9999

enum YMD {Year, Month, Day};

struct YMDInfo
{
	YMD m_ymd;
	RECT m_rcBound;
	CString m_strYMD;
	UINT m_uiFirstCharPos, m_uiLastCharPos;
	BOOL m_ChrInput;

	YMDInfo() : m_ymd(Year), m_strYMD(), m_uiFirstCharPos(0), m_uiLastCharPos(0), m_ChrInput(FALSE)
	{
		::ZeroMemory(&m_rcBound, sizeof(RECT)); 		
	}
};

class CCellDateCtrl : public CCellDropDown  
{
public:
	static void SetDateFormat(const CString &strDateFormat);

	CCellDateCtrl();
	virtual ~CCellDateCtrl();
	virtual void DrawCtrl(CDC *pDC, const LPRECT prcCell, UINT uiItemState = 0);
	virtual int GetMinWidth();
	virtual void AddToCtrlTypeStyle(DWORD dwFlags);
	virtual void RemoveFromCtrlTypeStyle(DWORD dwFlags);
	virtual LONG GetStyle();
	virtual void OnOpenDropDown();
	virtual void GetDropDownBoundingRect(RECT &rc, BOOL downwards /*= TRUE*/);
	virtual void OnSelCloseDropDown(LPARAM lParam);
	virtual void OnSelChangeDropDown(LPARAM lParam);
	virtual void OnKillActive();
	void SetYMD(WORD Y, WORD M, WORD D);
	virtual void OnThemeChanged();
protected:
	virtual BOOL OnLButtonDown(UINT nFlags, CPoint point);
	virtual BOOL OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	virtual void Initialize(HWND hParentWnd, BOOL *pParentEnabled, LPCTSTR strText = _T(""));
	BOOL DrawThemeDatePicker(HDC hDc, const LPRECT prcCell);
private:
	static DWORD	m_dwDateCtrlStyle;
	static BOOL		m_DateFormatInitialized, m_StartSeparator;
	static CString  m_DtFormatStr;
	static CStringArray	m_Separators;
	static BOOL CALLBACK InitDateFormatProc(LPTSTR lpDateFormatString, CALID CalId);
	inline static BOOL IsYMD(TCHAR tchar);
	static void ComputeSeparators();
	static void InitializeDateFormat();
	
	CArray<YMDInfo> m_YMDInfoArray;
	int m_YMDActPos;
	SYSTEMTIME m_CellDate;
	BOOL m_YMDDetailsInit;
	CString	m_strDisplay;

	void ValidateYMDItem();
	void ComputeDisplayDate();
	void AddYMDInfo(YMDInfo &ymdInfo, TCHAR YMDch);
	void ComputeYMDDetails();
	void PopulateYMDDetails();
	void ComputeYMDRects(CDC *pDC, const RECT &rcTxt);
	void CorrectDay();
	void UpdateTextDate();
	virtual void OnKillFocus(CWnd* pNewWnd);
};

BOOL CCellDateCtrl::IsYMD(TCHAR tchar)
{
	return tchar == DAY_CH || tchar == MONTH_CH || tchar == YEAR_CH;
}

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