change background color for multiline CEdit

I need to change background color for multiline CEdit. I followed many examples available to overwrite OnCtlColor, and that only partially worked.
For example my multiline CEdit has 10 lines, and first 5 lines have text. OnCtlColor would only change background color for the first 5, and the rest remain the Windows default color.

How do I make sure all lines got changed color at once?

Probably a common question, but just cant find answers easily. :(

Thanks all.
oldyellowcatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
You have to handle WM_ERASEBKGND message, but there are some considerations. Have a look to this previous question:
http://www.experts-exchange.com/Programming/Programming_Languages/MFC/Q_20779791.html
0
Amritpal SinghCommented:
try this

1. Add a HBRUSH member to your dialog class. Let us call it m_hEditBrush.
2. Initialize this HBRUSH in OnInitDialog by calling
m_hEditBrush = CreateSolidBrush(RGB(70,0,50));
3. Return this brush in OnCtlColor instead of hbr, if the control in
question is the desired one:
...
case CTLCOLOR_EDIT:
case CTLCOLOR_MSGBOX:
// Set color to green on black and return the background brush.
    pDC->SetTextColor(RGB(0, 255, 0));
    pDC->SetBkColor(RGB(70, 0, 50));
    return m_hEditBrush;
...

4. Do a DeleteObject of the brush in OnDestroy, DeleteObject(m_hEditBrush)

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Amritpal SinghCommented:
also have a look at the example at the following llink
it worked for multiline CEdit control

http://www.funducode.com/freevc/controls/controls2.htm

the useful code at the link :
HBRUSH CChildView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
      HBRUSH hbr = CWnd ::OnCtlColor(pDC, pWnd, nCtlColor);
      
      
      CBrush brush ;
      
      pDC ->SetBkMode ( TRANSPARENT ) ;
      brush.CreateStockObject ( NULL_BRUSH ) ;
      
      return (HBRUSH) brush.m_hObject ;      
      // TODO: Return a different brush if the default is not desired
      return hbr;
}

int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
      if (CWnd ::OnCreate(lpCreateStruct) == -1)
            return -1;
      
      m_edit.Create ( ES_MULTILINE|WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL, CRect ( 10, 10, 200, 140 ), this, ID_EDIT1 ) ;      
      return 0;
}
0
oldyellowcatAuthor Commented:
Thanks everyone, especially Amrit_82. Code works beautifully.
Turned out my OnCtlColor is almost exactly the same as yours, but I was returning the background brush incorrectly. Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.