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
cneatonAsked:
Who is Participating?
 
hakobConnect With a Mentor Commented:
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

--------------------------------------------------------------------------------


SUMMARY
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().



MORE INFORMATION
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
      public:
          CEditDialog(CWnd* pParent = NULL);    // standard constructor

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

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

      // Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CEditDialog)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
      //}}AFX_VIRTUAL

      // Implementation
         protected:

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

      // 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__;
      #endif

   //////////////////////////////////////////////////////////////////////
      // CEditDialog dialog

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

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

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

      BEGIN_MESSAGE_MAP(CEditDialog, CDialog)
          //{{AFX_MSG_MAP(CEditDialog)
          ON_WM_CTLCOLOR()
          ON_WM_DESTROY()
          //}}AFX_MSG_MAP
      END_MESSAGE_MAP()

//////////////////////////////////////////////////////////////////////
      // 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
                 brush.
              pDC->SetTextColor(RGB(0, 255, 0));
              pDC->SetBkColor(RGB(0, 0, 0));
              return (HBRUSH)(m_pEditBkBrush->GetSafeHandle());

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

      void CEditDialog::OnDestroy()
      {
          CDialog::OnDestroy();

          // Free the space allocated for the background brush
          delete m_pEditBkBrush;
      }
0
 
captainkirkCommented:
0
 
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
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
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.

Thanks!
0
 
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
{
protected:
     COLORREF m_clrText;
     COLORREF m_clrBack;
     CBrush m_brBkgnd;

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

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

     DECLARE_MESSAGE_MAP()
};

//////////////////////IMPLEMENTATION
#include "ColorEdit.h"

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

ColorEdit::ColorEdit()
{
     m_hbrBack = ::CreateSolidBrush(RGBRED);
}

ColorEdit::~ColorEdit()
{
     ::DeleteObject(m_hbrBack);
}

BEGIN_MESSAGE_MAP(ColorEdit, CEdit)
     ON_WM_CTLCOLOR_REFLECT()
END_MESSAGE_MAP()

ColorEdit::ColorEdit ()
{
     m_clrText = RGBBLACK;
     m_clrBack = ::GetSysColor(COLOR_3DFACE);
     m_brBkgnd.CreateSolidBrush(m_clrBack);
}

void ColorEdit::SetTextColor(COLORREF clrText)
{
     m_clrText = clrText;
     Invalidate();
}

HRBRUSH ColorEdit::CtlColor (CDC* pDC, UINT nCtlColor)
{
     pDC->SetTextColor(m_clrText);
     pDC->SetBkColor(m_clrBack);
     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!!

0
 
PacmanCommented:
include MFC-headers !
0
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.

All Courses

From novice to tech pro — start learning today.