Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing text color in a Dialog edit box

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

618 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