?
Solved

Changing text color in a Dialog edit box

Posted on 1998-10-01
2
Medium Priority
?
265 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 400 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

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!

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

800 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