Solved

changing text color URGENT

Posted on 1998-09-01
2
139 Views
Last Modified: 2013-11-19
I have a dialog with a lot of edit control.I want to change the text color to red of only one edit control.How can I do it?
0
Comment
Question by:erp
[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 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1321562
Override the CWnd::OnCtlColor. Look into the MFC Technical Note "TN062: Message Reflection for Windows Controls".
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1321563
Your dialog box will receive WM_CTLCOLOREDIT messages for each edit control in your dialog.  What you do is add a handler for this message and look for the specific control you are interested in (and change it's text color):

STEPS
1. Add WM_CTLCOLOR handler using class wizard.  You'll get a funciton something like:

HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
      HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
      
      // TODO: Change any attributes of the DC here
      
      // TODO: Return a different brush if the default is not desired
      return hbr;
}

2. Now the 2nd parameter pWnd tells you which control the message is for, and the 3rd parameter tells you which type of control.  Add a test for either or both of these, and using SetTextColor on the DC to change the foreground color.

HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

 if ( pWnd->GetDlgCtrlID() == ID_MYEDIT )
 {
   pDC->SetTextColor( RGB(255,0,0) ) ; // changes to text red
 }
      
 return hbr;
}

3. You can also change the background color of the edit control by returning an appropriate brush (which is used for the "non text" areas of the control) and using pDC->SetBkColor(..etc..) (which is used for the "text" areas of the control).


A FINAL NOTE
1. If you find yourself doing this a lot you may prefer to make your own class derived from CEdit which handles the WM_CTLCOLOR message (it will receive it thru message reflection) and has extra members for setting the background/foreground colors.
2. This is documented in "TN062" in the MFC help (simply type this into the Index tab of the dialog which appears when you click Help/Search menu)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

635 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