Solved

How to Change color of a CEdit

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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