Link to home
Start Free TrialLog in
Avatar of geomvigla
geomvigla

asked on

Dialog boxes-change the background an edit box

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
Avatar of DanRollins
DanRollins
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial