Solved

Changing text color in a Dialog edit box

Posted on 1998-10-01
2
254 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
if loop error 4 77
viewing source code from eclipse 13 74
fix34  challenge 9 97
Thin secure Windows 10 5 48
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…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now