erp
asked on
changing text color URGENT
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
STEPS
1. Add WM_CTLCOLOR handler using class wizard. You'll get a funciton something like:
HBRUSH CAboutDlg::OnCtlColor(CDC*
{
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*
{
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)