0

I have a dialog in which the edit box is disabled but the text should be displayed in red instead of the default grey. I tried the following:

void CMyEdit::OnEnable(BOOL bEnable)
{
  CEdit::OnEnable(bEnable);

  if (bEnable)
  {
    m_BackGroundColor = kRGBWhite;
  }
  else
  {
    m_BackGroundColor = kRGBDefaultGray;
  }

  m_TextColor = kRGBRed;
  m_BackgroundBrush.DeleteObject();
  m_BackgroundBrush.CreateSolidBrush(m_BackGroundColor);

  Invalidate();
}

But its still displaying the text in grey only. But if I remove the base class call CEdit::OnEnable(bEnable); then new text color takes effect. Can anyone explain whats wrong in the code?

Thanks for your time.

cheers...

EDIT: (Moving my comment to an edit) Sorry I forgot to mention it in my original post. Instead of handling the ON_WM_CTLCOLOR(), ON_WM_CTLCOLOR_REFLECT() is handled. What I don't understand is that removing the call to the base class, changes the color of the text to what's specified but keeping it changes it only to the default grey.

yasouser
  • 5,113
  • 2
  • 27
  • 41

2 Answers2

2

MSDN: An edit control that is not read-only or disabled sends the WM_CTLCOLOREDIT message to its parent window when the control is about to be drawn.

The corollary is there is no way to change the background or text color of a disabled (or readonly) edit box, besides subclassing it and do the painting yourself.

Axel Rietschin
  • 611
  • 1
  • 7
  • 10
  • thanks for your answer. Please see the comment I added to the question. – yasouser Jan 24 '11 at 14:12
  • I think read-only edits also send the WM_CTLCOLOREDIT message; I use readonly edits instead of disabled in order to control the coloring of them while readonly because of this. – aquirdturtle Dec 05 '18 at 00:40
1

You need to handle OnCtlColor handler and in your message map put this ON_WM_CTLCOLOR()

afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
 //logic here, change pDC as needed, return brush to use
}
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636