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

Changing Text Color

I've created a Dialog-based application (using Visual C++) in which users enter in information and submit it to be processed.  I have an edit control on the interface which will be used to display alert messages to the user if they enter in invalid information.  I want the error message to appear in red and then in black when a new error occurs.  However, I've been having a hard time finding information about changing text color in Edit Controls.  It seems alot of people are interested in changing the background color, but I want to change the text color only.

Any ideas??  Thanks
1 Solution
Meir RivkinFull stack Software EngineerCommented:
goto: http://www.codeguru.com/editctrl/CEditExAndCStaticEx.shtml

and download the demo project and source, its exactly what u need.

good luck
HOWTO: Change the Background Color of an MFC Edit Control
ID: Q117778


The information in this article applies to:

The Microsoft Foundation Classes (MFC), included with:
Microsoft Visual C++ for Windows, 16-bit edition, versions 1.0, 1.5, 1.51, 1.52
Microsoft Visual C++, 32-bit Editions, versions 1.0, 2.0, 2.1, 4.0, 4.1, 4.2, 5.0, 6.0


To change the background color of an edit control in an MFC application, you must override the OnCtlColor() message-handling function of the window containing the edit control.

In the new OnCtlColor() function, set the background color and return a handle to a brush that will be used for painting the background. This must be done in response to receiving both the CTLCOLOR_EDIT and CTLCOLOR_MSGBOX messages in the OnCtlColor() function.

This is also documented in the "Class Library Reference" under CWnd::OnCtlColor().

The sample code below uses a CDialog-derived class to demonstrate the process. Class Wizard was used to generate message-handling functions for the WM_CTLCOLOR and WM_DESTROY messages. These functions are called CEditDialog::OnCtlColor() and CEditDialog::OnDestroy(), respectively.

Sample Code

      // editdlg.h : header file

      // CEditDialog dialog

      class CEditDialog : public CDialog
      // Construction
          CEditDialog(CWnd* pParent = NULL);    // standard constructor

      // Add a CBrush* to store the new background brush for edit controls.
          CBrush* m_pEditBkBrush;

      // Dialog Data
          enum { IDD = IDD_EDITDIALOG };
              // NOTE: The ClassWizard will add data members here.

      // Overrides
      // ClassWizard generated virtual function overrides
      virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

      // Implementation

          // Generated message map functions
          afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
          afx_msg void OnDestroy();

      // editdlg.cpp : implementation file

      #include "stdafx.h"
      #include "mdi.h"
      #include "editdlg.h"

      #ifdef _DEBUG
      #undef THIS_FILE
      static char BASED_CODE THIS_FILE[] = __FILE__;

      // CEditDialog dialog

      CEditDialog::CEditDialog(CWnd* pParent /*=NULL*/)
          : CDialog(CEditDialog::IDD, pParent)
              // NOTE: The ClassWizard will add member initialization here.

          // Instantiate and initialize the background brush to black.
          m_pEditBkBrush = new CBrush(RGB(0, 0, 0));

      void CEditDialog::DoDataExchange(CDataExchange* pDX)
              // NOTE: The ClassWizard will add DDX and DDV calls here.

      BEGIN_MESSAGE_MAP(CEditDialog, CDialog)

      // CEditDialog message handlers

      HBRUSH CEditDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
          switch (nCtlColor) {

          case CTLCOLOR_EDIT:
          case CTLCOLOR_MSGBOX:
              // Set color to green on black and return the background
              pDC->SetTextColor(RGB(0, 255, 0));
              pDC->SetBkColor(RGB(0, 0, 0));
              return (HBRUSH)(m_pEditBkBrush->GetSafeHandle());

              return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

      void CEditDialog::OnDestroy()

          // Free the space allocated for the background brush
          delete m_pEditBkBrush;
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

cneatonAuthor Commented:
Thanks for the code, hakob.  But where would (or how) do I call the function to actually change the color.   I want to display the text in red initially so I would have to call the function to invoke a change in text color (black -> red)  and then I want to set the text back to black.  How do I do that?  I see the code that would be used to change the color.  I just need to know how to invoke it.

cneatonAuthor Commented:
I found some code that could be used to enhance the CEdit class in order to allow text color changes.  However, when I tried to run it, I got errors. Here is the code:

//////////////////////HEADER FILE

class ColorEdit : public CEdit
     COLORREF m_clrText;
     COLORREF m_clrBack;
     CBrush m_brBkgnd;

     void SetTextColor (COLORREF clrText);
     void SetBkColor (COLORREF clrBack);
     HBRUSH m_hbrBack;
     virtual ~ColorEdit();

     afx_msg HBRUSH ONCtlColor(CDC *pCD, CWnd* pWnd, UINT nCtlColor);


#include "ColorEdit.h"

#define RGBRED    RGB(128,0,0)
#define RGBWHITE  RGB(255,255,255)
#define RGBBLACK  RGB(0,0,0)

     m_hbrBack = ::CreateSolidBrush(RGBRED);



ColorEdit::ColorEdit ()
     m_clrText = RGBBLACK;
     m_clrBack = ::GetSysColor(COLOR_3DFACE);

void ColorEdit::SetTextColor(COLORREF clrText)
     m_clrText = clrText;

HRBRUSH ColorEdit::CtlColor (CDC* pDC, UINT nCtlColor)
     return (HBRUSH)m_brBkgnd;

I think the most significant error I got was:
error C2504: 'CEdit' : base class undefined

I called it the most significant error because I think that correcting this problem would eliminate a host of others.  Any suggestions as to what I'm doing wrong?

Thanks so much!!

include MFC-headers !
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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