Solved

Changing text color in a Dialog edit box

Posted on 1998-10-01
2
262 Views
Last Modified: 2013-11-20
I would like to be able to dynamically change the color of the text in my dialog edit ctrl.

For example:

  In my modeless dialog, if the user enters vaild data and presses OK the text in the edit ctrl would turn green otherwise it would turn read.


Thanks.

-Eric






0
Comment
Question by:nielsew
[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
  • 2
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
ID: 1322816
Override the WM_CtlColor message to change the color of the control.

You can do this in the dialog, or derive a class from CEdit (and override the reflection version which has an = sign before the name).

When you override this message you
1. Can set the text color using
pDC->SetTextColor( RGB(whatever) ) ;

2. Can set the background using
pDC->SetBkColor( RGB(whatever) ) ;
and returning an HBRUSH wish a solid color


Therefore the steps are
A. Add an HBRUSH to the dialog class as a member variable
B. In your constructor create a brush or brushes

m_hBrush = ::m_hbr = CreateSolidBrush( RGB(whatever) ) ;

C. In your destructor destroy the brush or brushes

DeleteObject( m_hBrush ) ;

D. Override the WM_CTLCOLOR message.  You can check it's the control you're interesting in by checking the pWnd and nCtlColor values

example
HBRUSH CMyDialog::OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor )
{
   if ( _red_ )
   {
     pDC->SetTextColor( RGB(255,0,0) ) ;
     pDC->SetTextColor( RGB(255,255,255) ) ;
     return m_hBrush ;
   }

   // etc
}

E. When you want to force a repaint of the particular control set a flag indicating if it's red, green or whatever, then force a repaint as

GetDlgItem(ID_MYEDIT)->InvalidateRect( NULL, TRUE ) ;
GetDlgItem(ID_MYEDIT)->UpdateWindow() ;

F. So you know RGB values you  might find useful

RGB(255,0,0) =  bright red
RGB(0,0,255) = bright green
RGB(255,255,255) = white
RGB(0,0,0) = black

RGB(128,0,0) = dark red
RGB(0,0,128) = dark green

0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1322817
This m_hBrush = ::m_hbr = CreateSolidBrush( RGB(whatever) ) ;

should have said

m_hBrush = ::CreateSolidBrush( RGB(whatever) ) ;

example

m_hBrushWhite = ::CreateSolidBrush( RGB(255,255,255) ) ;

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

696 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