« November 2005 »
S M T W T F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
You are not logged in. Log in

Programming Tips
Thursday, 10 November 2005
Quick way of changing background color of edit controls
Mood:  cheeky
Topic: VC++
Windows provides us a way to override the look and feel of a component at runtime throught this method. 
For more information take a look at the MSDN documenation on 
OnCtlColor().

The nCtlColor parameter contains the category of the control being painted, for eg:

CTLCOLOR_BTN -- Button control 
CTLCOLOR_DLG -- Dialog box 
CTLCOLOR_EDIT -- Edit control 
CTLCOLOR_LISTBOX -- List-box control 
CTLCOLOR_MSGBOX -- Message box 
CTLCOLOR_SCROLLBAR -- Scroll-bar control 
CTLCOLOR_STATIC -- Static control 

Well if you want to check for the painting of a particular control then use 
pWnd->GetDlgCtrlID(), which returns the id of the control being painted.

The device of the context of the control being painted is in the pointer pDC.

This is how we go about it:

HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  //call base class implementation first otherwise it may undo what we have done.
  HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

  //Get the ctrl id and check whether we are painting the edit control or not. 
  if (pWnd->GetDlgCtrlID() == IDC_OF_EDITBOX)
  {
    // Set the text color to red.
    pDC->SetTextColor(RGB(255, 0, 0));

    //set to trasparent mode
    pDC->SetBkMode(TRANSPARENT);

    //unless we do this there won't be any effect.
    hbr = m_brush;
  }
  return hbr;
}

Posted by Nibu babu thomas at 11:05 AM
Updated: Thursday, 10 November 2005 4:47 PM

View Latest Entries