Altering the Background and Text Color of a Control

A Simple Way to Alter the Background and Text Color of a Control (VC++)

, Vienna (Austria).

Environment: VC6

  1. Start by adding a handler for message WM_CTLCOLOR to the window containing the control (usually a CDialog derived class).
  2. Then, assuming m_colorBk to be the variable storing the desired background color of your control, add a block of code like this:
    HBRUSH CSomeDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
      HBRUSH hbr;
      hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    
      CBrush Brush;
      COLORREF MyColor;
      MyColor = m_colorBk;
    
      // TODO: Change any attributes of the DC here
      if (nCtlColor == CTLCOLOR_EDIT)
      {
         Brush.CreateSolidBrush(MyColor);
         pDC->SetBkMode(TRANSPARENT);
         pDC->SetBkColor(MyColor);
    
         // Set the text color to something that can be read on the
         // given background color
         COLORREF rgbText;
         int r, g, b, rt, gt, bt;
         div_t Result;
         Result = div(m_colorBk, 256);
         r = Result.rem;    rt = 255 - r;
         Result = div(Result.quot, 256);
         g = Result.rem;    gt = 255 - g;
         b = Result.quot;   bt = 255 - b;
         if ((abs(rt-r)<50) && (abs(gt-g)<50) && (abs(bt-b)<50))
            rgbText = RGB(0,0,0);
         else
            rgbText = RGB(rt,gt,bt);
         pDC->SetTextColor(rgbText);
         hbr = (HBRUSH)Brush;
         Brush.DeleteObject();
      }
    
      // TODO: Return a different brush if the default is
      //       not desired
      return hbr;
    }
    

    As you perhaps noticed, I used the div() function to compute the quotient and the remainder when dividing one integer by another. So, do not forget to include "math.h"!

  3. Create a new Class derived from the control class (let's say an edit control).
  4. Declare a variable of the new type as a member of your CSomeDlg class.
       CMyClass m_edit_1;
    
  5. Map a variable to the control (let's say m_strEdit_1).
  6. Subclass the control (within the OnInitDialog for a dialog derived class).
       m_edit_1.SubclassDlgItem(IDC_EDIT_1, this);
    

    where IDC_EDIT_1 is the resource ID of your control.

  7. Add a handler for message WM_ERASEBKGND to the new CMyClass class, with a code block like this:
    BOOL CMyClass::OnEraseBkgnd(CDC* pDC) 
    {
       // TODO: Add your message handler code here and/or call default
       CSomeDlg *pMain = (CSomeDlg *)AfxGetMainWnd();
    
       CRect rcClient;
       GetClientRect(&rcClient);
       CBrush Brush;
       COLORREF MyColor;
       MyColor = pMain->m_colorBk;
       Brush.CreateSolidBrush(MyColor);
       pDC->FillRect(&rcClient, &Brush);
       Brush.DeleteObject();
       pDC->SetBkColor(MyColor);
    
       return CEdit::OnEraseBkgnd(pDC);
    }
    
  8. Add a handler for message =EN_CHANGE to the new CMyEdit new class, with a code block like this:
    void CMyClass::OnChange() 
    {
       // TODO: If this is a RICHEDIT control, the control will not
       // send this notification unless you override the 
       // CEdit::OnInitDialog()
       // function and call CRichEditCtrl().SetEventMask()
       // with the ENM_CHANGE flag ORed into the mask.
    
       // TODO: Add your control notification handler code here
       CSomeDlg *pMain = (CSomeDlg *)AfxGetMainWnd();
       pMain->UpdateData(true);
    }
    

    The code above is particularly important when the system tries to update your application's window.

    If you forget that, you'll be disapointed seeing that your text within the edit control has vanished when your application's window again becomes top most on the screen!

Downloads

Download demo - 19.1 Kb
Download source - 21.3 Kb


Comments

  • SliderControl?

    Posted by Legacy on 07/15/2003 12:00am

    Originally posted by: ManjuG

    How can we change the background of a slider control?

    Reply
  • how is it different

    Posted by Legacy on 06/16/2002 12:00am

    Originally posted by: meeaooow

    hpw is it diferent than the example provided in the online help?

    they have a control called YellowEdit in the example.

    in think it appears under "message reflection", TN86 or so.

    thanks

    Reply
  • Forgot to take out a line after some handle testing

    Posted by Legacy on 06/14/2002 12:00am

    Originally posted by: Mircea

    Please comment one line within the OnCtlColor():

    // hbr = (HBRUSH)Brush;

    Reply
  • color problem.

    Posted by Legacy on 06/13/2002 12:00am

    Originally posted by: yalcin

    Probably I am doing stg wrong, But when I run the test app and right click and select the color it is the text color that changes and in a negative fashine. So right click select white gives you black text on white bck. Right click select white gives you white text on white backgr. On W2K system.

    haven't looked at source yet.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Temporary network outages -- even those lasting just a few minutes -- can cripple organizations that rely on local Internet access at remote locations. Failover systems that automatically switch to 4G LTE cellular when landlines go down let organizations maximize uptime, even when the unexpected happens. Read this white paper to learn how 4G failover systems that use standalone gateways supported by a cloud-based management tool are proving to be a solid choice for improved business continuity and productivity …

  • Following an IT incident through to resolution is more than just acknowledging an alert and restarting a server. The recent State of On-Call Report found that it takes most companies an average of 10-30 minutes to resolve an incident with an average of 5 people involved. Imagine how much money and time companies are spending to deal with incident resolution. But what if you had a tool that offered solutions baked in? Or a tool that sent alerts to the right person or team every time? These are the kind of …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date