Solved

Changing text color in a Dialog edit box

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change owner from userA to server Local Administrators 7 59
move a line in eclipse 3 81
VBA "SendKeys" Syntax for Multiple Keystrokes 7 103
scoreUp challenge 14 56
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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

17 Experts available now in Live!

Get 1:1 Help Now