Click here to Skip to main content
6,635,160 members and growing! (15,716 online)
Email Password   helpLost your password?
Desktop Development » Edit Controls » Owner drawn controls     Intermediate

How to change the back color of an edit box when it gets the focus

By Warren J. Hebert

Change the back color of an edit box to yellow when it gets the focus, and back to white when it loses the focus.
VC6Win2K, MFC, Dev
Posted:10 Jun 2001
Views:126,965
Bookmarked:39 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
20 votes for this article.
Popularity: 3.36 Rating: 2.58 out of 5
5 votes, 31.3%
1
3 votes, 18.8%
2
2 votes, 12.5%
3
2 votes, 12.5%
4
4 votes, 25.0%
5

Sample Image - CEditBkColor.gif

Introduction

This article shows how to change the back color of an edit box when it gets the focus.

  1. Derive a class from the standard edit control class, CEdit.
  2. Add 3 private variables:
    • A COLORREF to store the current back color
    • A CBrush to store the brush used to paint the back color of the edit control
    • A CString to store the current contents of the edit box
  3. Override the virtual OnChildNotify function.
    BOOL CMyEdit::OnChildNotify(UINT message, 
        WPARAM wParam, LPARAM lParam, LRESULT* pLResult)
    {
        // We only want to handle WM_CTLCOLOREDIT messages - 32 bit only
    
        if (message != WM_CTLCOLOREDIT) 
        {
            return CEdit::OnChildNotify(message, wParam, lParam, pLResult);
        }
        HDC hdcChild = (HDC)wParam;
        // Text is black � you can modify this by adding
    
        // another variable for text color
    
        SetTextColor(hdcChild, RGB(0,0,0));
        SetBkColor(hdcChild, m_BackColor);
        return TRUE;
    }
  4. Add message handlers to handle the following Windows messages:
    • ON_WM_SETFOCUS
    • ON_WM_KILLFOCUS
    • ON_WM_PAINT
    void CMyEdit::OnSetFocus(CWnd* pOldWnd)
    {
        CEdit::OnSetFocus(pOldWnd);
    
        m_BackColor = RGB(255,255,0);
        Invalidate(FALSE);
    }
    
    void CMyEdit::OnKillFocus(CWnd* pNewWnd)
    {
        CEdit::OnKillFocus(pNewWnd);
        // I am setting normal back color to white
    
        // � you can modify this as you see fit
    
        m_BackColor = RGB(255,255,255);
        // Force a repaint
    
        Invalidate(FALSE);
    }
    void CMyEdit::OnPaint()
    {
        CPaintDC dc(this); // device context for painting
    
    
        // TODO: Add your message handler code here
    
        GetWindowText(m_Text);
        SetBkGrndColor();
    
        // Do not call CEdit::OnPaint() for painting messages
    
    }
    
  5. Add a private member function SetBkGrndColor()
    void CMyEdit::SetBkGrndColor()
    {
        // Delete the old brush
    
        m_Brush.DeleteObject();
        // Create a new brush in the specified color
    
        m_Brush.CreateSolidBrush(m_BackColor);
        CDC* pDC = GetDC();
        pDC->SetBkMode(OPAQUE);
        pDC->SetBkColor(m_BackColor);
        // Select the brush into this window�s device context
    
        pDC->SelectObject(&m_Brush);
        CRect rc;
        // Get the client area of the edit control
    
        GetClientRect(&rc);
        ScreenToClient(&rc);
        // Apply the device context to the client area of the edit control
    
        pDC->Rectangle(0, 0, rc.Width(), rc.Height());
        // Rewrite the text since the backcolor paint
    
        // overwrote the existing text
    
        pDC->SetTextColor(RGB(0, 0, 0,));
        pDC->TextOut(2, 2, m_Text.GetBuffer(m_Text.GetLength()));
    }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Warren J. Hebert


Member
I am a Visual Basic, Visual C++ programmer (consultant) with 6 years experience. I am also an instructor and have been teaching Visual Basic and Visual C++ at a local community college for the past 15 months.
Occupation: Web Developer
Location: United States United States

Other popular Edit Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
Generalin my opinion... PinmemberCho,Dae-Wan8:46 27 Mar '04  
Generalhow to change the color of an edit box Pinmemberbhanusiva7:04 29 Jan '04  
GeneralRe: how to change the color of an edit box Pinmemberihoapm21:36 23 Dec '04  
GeneralSeems to work without CMyEdit::OnPaint Pinmemberchris1090:55 17 Dec '03  
GeneralThis article is just cut-n-paste from MSDN Q132080 Pinmemberip22:05 30 Oct '03  
GeneralRe: This article is just cut-n-paste from MSDN Q132080 PinmemberDean Bathke19:19 4 Feb '05  
GeneralChanging the Selection Color within the control PinmemberChandra Birusumanti9:47 12 Aug '03  
GeneralQuestion about CEdit PinmemberAlex Bikov9:46 1 Feb '02  
GeneralProblems PinmemberChristian Graus14:28 11 Nov '01  
GeneralRe: Problems PinmemberJames R. Twine9:16 4 Jan '02  
GeneralRe: Problems PinmemberChristian Graus11:50 4 Jan '02  
GeneralRe: Problems PinmemberJames R. Twine12:01 4 Jan '02  
GeneralRe: Problems PinmemberChristian Graus12:10 4 Jan '02  
GeneralRe: Problems TN062 Pinmemberosy2:28 8 Apr '03  
GeneralMore options PinmemberKant10:05 16 Jun '01  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Jun 2001
Editor: Smitha Vijayan
Copyright 2001 by Warren J. Hebert
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project