?
Solved

Changing Text Color

Posted on 2001-07-16
6
Medium Priority
?
347 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:cneaton
[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
6 Comments
 
LVL 4

Expert Comment

by:captainkirk
ID: 6285951
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 6285984
goto: http://www.codeguru.com/editctrl/CEditExAndCStaticEx.shtml

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

good luck
0
 
LVL 1

Accepted Solution

by:
hakob earned 200 total points
ID: 6286999
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:cneaton
ID: 6287325
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
 

Author Comment

by:cneaton
ID: 6287505
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
 
LVL 9

Expert Comment

by:Pacman
ID: 6293401
include MFC-headers !
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

650 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