change background color for multiline CEdit

Posted on 2004-11-17
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
    LVL 55

    Expert Comment

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

    Accepted Solution

    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)

    LVL 6

    Expert Comment

    by:Amritpal Singh
    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
    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.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now