• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

How to Change color of a CEdit

I would like to change the color of a CEdit control. Can it be done?
Please help..
0
vardis
Asked:
vardis
  • 3
1 Solution
 
vardisAuthor Commented:
Adjusted points to 20
0
 
jubjubCommented:
Catch the OnCtlColor message of the CWnd
// Code
if(nCtlColor == CTLCOLOR_EDIT)
{
  pDC->SetBkColor(RGB(255,255,255));
  return CreateSolidBrush(RGB(255,255,255));
}
0
 
vardisAuthor Commented:
Thank you jubjub for the answer
The code works fine but there is a problem when it is executed it changes the color of ALL CEdit controls
Is there a way of changing color of just one?

Thanks
0
 
paynCommented:
To change the color of just one CEdit, match the HWNDs. I know that, looking at the prototype (and documentation) for OnCtlColor, it looks like the UINT nCtlColor is very useful and the CWnd* is useless, but unless you're trying to, e.g., color all edit windows the same color, the reality is the other way around. The CWnd* points to a CWnd object that wraps your control's HWND.

Try this:

HBRUSH CMyWnd::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  if (pWnd->GetSafeHwnd() == m_editToColor.GetSafeHwnd()
  {
    pDC->SetTextColor(m_clrText);
    pDC->SetBkColor(m_clrBack);
    return CreateSolidBrush(m_clrBack);
  }
}

Note that you'll need to attach a (CEdit) member variable to your edit control for this to work. If you don't want to do that, you can replace the "m_editToColor." above with "GetDlgItem(IDD_EDIT_TO_COLOR)->" and it'll work.

Under certain circumstances the CWnd* will actually be a pointer to your (CEdit) member variable. MFC internally keeps a map of handles to object, and if your member is in that map you'll get it back. If it's not, you'll get a temporary CWnd object instead, which will be destroyed as soon as the function returns.

By the way, if you have lots of controls to color, you may want to consider making a map of HWNDs to colors. I usually use code like this to do the actual coloring:

HBRUSH CMyClass::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
      std::map<HWND, pair<COLORREF, COLORREF >::
            const_iterator itControl =
                  m_mapControlColor.find(pWnd->m_hWnd);
      if (itControl == m_mapControlColor.end())
            return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

      pDC->SetTextColor(itControl->second.first);
      pDC->SetBkColor(itControl->second.second);
      return CreateSolidBrush(itControl->second.second);
}

Then, somewhere during initialization, you fill this map, using calls like this:
      m_mapControlColor[m_staticCaption.m_hWnd] =
            pair<RGB(0,0,0), RGB(0,0xff,0xff);

If you prefer CMap to STL's maps, they work fine too. You can even wrap these definitions up in MFC-esque macros like DEFINE_CONTROL_COLOR and IMPLEMENT_CONTROL_COLOR if you plan to use this in many separate applications...

0
 
vardisAuthor Commented:
Thank you payne for a very informative and detailed explanation I'll try your suggestion/solution. It has answered my question.

Regards Vardis.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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