• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • 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
0
cneaton
Asked:
cneaton
1 Solution
 
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
 
hakobCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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