?
Solved

How to Change color of a CEdit

Posted on 1998-08-04
5
Medium Priority
?
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 80 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

801 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