[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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
0
geomvigla
Asked:
geomvigla
  • 2
1 Solution
 
DanRollinsCommented:
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
0
 
DanRollinsCommented:
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now