Learn how to a build a cloud-first strategyRegister Now


change background color for multiline CEdit

Posted on 2004-11-17
Medium Priority
Last Modified: 2013-11-20
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.
Question by:oldyellowcat
  • 2
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12610169
You have to handle WM_ERASEBKGND message, but there are some considerations. Have a look to this previous question:

Accepted Solution

Amritpal Singh earned 500 total points
ID: 12611394
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:
// 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)


Expert Comment

by:Amritpal Singh
ID: 12611441
also have a look at the example at the following llink
it worked for multiline CEdit control


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;

Author Comment

ID: 12611880
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.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question