Click to See Complete Forum and Search --> : ! Colored Edit Boxes !


EasyRhino
October 7th, 1999, 08:35 PM
Hi all,

I've got a simple class that colors the background and text of edit boxes. I'm writing a program in which I want to change those colors dynamically during execution. Here is the class implementation:

CColoredEdit::CColoredEdit()
{
// default colors //
m_colorText = RGB( 255, 255, 255 );
m_colorBkgnd = RGB( 70, 85, 120 );
m_brBkgnd.CreateSolidBrush( m_colorBkgnd );
}

CColoredEdit::CColoredEdit(unsigned long colorText, unsigned long colorBkgnd)
{
m_colorText = colorText;
m_colorBkgnd = colorBkgnd;
m_brBkgnd.CreateSolidBrush( m_colorBkgnd );
}

CColoredEdit::~CColoredEdit()
{
}

void CColoredEdit::SetColors(unsigned long colorText, unsigned long colorBkgnd)
{
m_colorText = colorText;
m_colorBkgnd = colorBkgnd;
}

BEGIN_MESSAGE_MAP(CColoredEdit, CEdit)
//{{AFX_MSG_MAP(CColoredEdit)
ON_WM_CTLCOLOR_REFLECT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CColoredEdit message handlers

HBRUSH CColoredEdit::CtlColor(CDC* pDC, UINT nCtlColor)
{
pDC -> SetTextColor( m_colorText );
pDC -> SetBkColor( m_colorBkgnd );

return m_brBkgnd;
}




The SetColors() function shown above does not have the desired effect. Can anyone tell me how to accomplish this??

BONUS QUESTION: how do I make the text in the edit box bold?

Thanks for any and all help!
Regards,
Chris

Rudolf
October 7th, 1999, 09:52 PM
Hi,
the control is not redrawn when You call SetColors. So the control has the color You want it to have but You can only see an older state. Call one of the functions that cause the edit box to redraw (I never know which one to use).
You can test whether it is the only reason (I can't see another one) in hiding the control behind another window and removing this window then.
HTH Rudolf

Sam Hobbs
October 7th, 1999, 10:14 PM
I did something very similar to yours except my SetColors looks like:

void CBlueEdit::SetColors(COLORREF Fg, COLORREF Bg) {
m_clrText = Fg;
m_clrBkgnd = Bg;
if ((HBRUSH)m_brBkgnd)
m_brBkgnd.DeleteObject();
m_brBkgnd.CreateSolidBrush(m_clrBkgnd);
}



I am not sure if the DeleteObject is necessary, though.

And I define m_clrText and m_clrBkgnd as COLORREF.

EasyRhino
October 7th, 1999, 10:57 PM
Great! Thanks a lot, works like a charm.

- chris