Solved

How to Change color of a CEdit

Posted on 1998-08-04
5
319 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 163
Expand data scrubbing tool 13 35
either24  challenge 19 101
Increment column based of a FK 8 31
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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