Dialog boxes-change the background an edit box

Posted on 2006-05-16
Last Modified: 2010-04-24
Hi,I create a dialog based project and  I want to change the background of an edit box when it takes a value.please, can you tell me how can I do it?If you can, send me the code of doing this.THANK you
Question by:geomvigla
    LVL 49

    Expert Comment

    Open the ClassWizard (ctrl+W)  
     Under the Message Maps tab, Select the main dialog class object
     In Messages, select  WM_CTLCOLOR.
     Click [Add Function]
     Click [Edit Code]
     Make it so:

    HBRUSH CD31Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
          HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
          if ( pWnd->m_hWnd == GetDlgItem( IDC_EDIT1 )->m_hWnd ) {
                if ( m_fEditIsRed ) {
                      pDC->SetTextColor( RGB(255,255,255) );  // white
                      pDC->SetBkColor( RGB(255,0,0) ); // red
                      return( m_hbrEditBkgd );
          return hbr;

    Back in the ClassWizard (Ctrl+W)
    Under the Message Maps tab... Select the main dialog class object
    In the Object IDs, select IDC_EDIT1 (or whatever is the ID of the edit box)
     In Messages, select  EN_CHANGE
     Click [Add Function]
     Click [Edit Code]
     Make it so:

    void CD31Dlg::OnChangeEdit1()
          m_fEditIsRed= TRUE;
          ::DeleteObject(m_hbrEditBkgd );
          m_hbrEditBkgd= ::CreateSolidBrush( RGB(255,0,0) );  // Red (changed)
          GetDlgItem( IDC_EDIT1 )->Invalidate();

    Scroll up to the OnInitDialogBox fn in theis main dlg cpp file.  Add these lines:

          m_hbrEditBkgd=     ::CreateSolidBrush( RGB(255,255,255) );  // white (unchanged)
          h_fEditIsRed= FALSE;

    Open the .H file for the main dialog.  Add this into the class def:
          HBRUSH m_hbrEditBkgd;
          BOOL     m_fEditIsRed;

    Now, as soon as the user begins typing, the background of that edit control will change to RED.

    If you want to wait until the user finishes typing, comment out the stuff in OnChangeEdit1 and use the ClassWizard to create a handler for EN_KILLFOCUS for IDC_EDIT1.  Make the code of you OnKillFocusEdit1() fn do the same as described above for OnCHangeEdit1.

    -- Dan
    LVL 49

    Accepted Solution

    If the user tabs around and comes back to that edit box, you might want it to go back to white.  If so, add a handler for EN_SETFOCUS and do the reverse-- turn off m_fEditIsRed, and set the brush to white.  You will need to do corresponding checking of m_fIsEditRed (and color reversal) in your OnCtlColor() fn.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    755 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

    19 Experts available now in Live!

    Get 1:1 Help Now