Gold mine of Visual C++ tricks!
how to set color for static control text in dialog?
![]()
Colors convey meanings too. For instance, if something is written in red – that means something to be cautious. If its in green, its treated as safe. Well, usually in window dialogs, the static control texts are black in color. Is it possible to color them to convey more meaning?

![]()
Yes! You have to handle WM_CTLCOLOR message. For each control, this message will be triggered and you’re free to do modification on your control. Have a look at the code snippet in MFC framework.
// Message Map
BEGIN_MESSAGE_MAP(CDlgDlg, CDialog)
...
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
HBRUSH CDlgDlg::OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor )
{
// Call base class version at first. Or else it will override your changes.
HBRUSH hbr = CDialog::OnCtlColor( pDC, pWnd, nCtlColor );
// Check whether which static label its.
if( pWnd->GetDlgCtrlID() == IDC_STATIC_OK )
{
// Set color as red.
pDC->SetTextColor( RGB( 255, 0, 0 ));
pDC->SetBkMode( TRANSPARENT );
}
return hbr;
}
![]()
Always call CDialog::OnCtlColor() at first. Or else it will override your modifications.
![]()
Targeted Audience – Intermediate.
| Print article | This entry was posted by Jijo Raj on August 27, 2008 at 6:00 pm, and is filed under Codeproject, Uncategorized. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 1 year ago
Hi, It really works, ive done it already, But im searching a messive way of the same thing to be done in simple WIN32, not MFC. Can u help me please
mehrangull@hotmail.com
about 1 year ago
If you seek plain win32, you could handle WM_CTLCOLORSTATIC and can change the static text color.
Have a look at this link – http://www.winprog.org/tutorial/dlgfaq.html. It contains code samples. Hope it helps.
Regards,
Jijo.
about 1 year ago
Is it applicable only to static control or i can use it for other
about 1 year ago
You could change the color of other controls as well. Controls such as – Button, Dialog, edit, Listbox, scrollbars etc. For instance, see how to change the color of push button here – http://www.daniweb.com/forums/thread26182.html
Regards,
Jijo.