8/5/08

Change background color in CEdit (MFC)

It was a real challenge for me to make a simple change of background color for CEdit. I was suprised to find out that there are no standard function, like CEdit::SetBkColor, to do this.

1. first step is to create class derived from CEdit and declare function CtlColor:

// CustomEdit.h
class CCustomEdit : public CEdit
{
protected:
HBRUSH CtlColor(CDC *pDC, UINT);
DECLARE_MESSAGE_MAP()
};


2. handle WM_CTLCOLOR_REFLECT event:

// CustomEdit.cpp
BEGIN_MESSAGE_MAP(CCustomEdit, CEdit)
ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()

HBRUSH CCustomEdit::CtlColor(CDC *pDC, UINT)
{
COLORREF bkColor = RGB(255, 255, 255);
pDC->SetBkColor(bkColor);
return CreateSolidBrush(bkColor);
}

No comments:

Post a Comment