Solved

How to Change color of a CEdit

Posted on 1998-08-04
5
312 Views
Last Modified: 2013-11-19
I would like to change the color of a CEdit control. Can it be done?
Please help..
0
Comment
Question by:vardis
  • 3
5 Comments
 

Author Comment

by:vardis
ID: 1320191
Adjusted points to 20
0
 

Expert Comment

by:jubjub
ID: 1320192
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
 

Author Comment

by:vardis
ID: 1320193
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
 
LVL 1

Accepted Solution

by:
payn earned 20 total points
ID: 1320194
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
 

Author Comment

by:vardis
ID: 1320195
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now