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.
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
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.
Is it applicable only to static control or i can use it for other
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.
You would be much better off writing a subclass of CStatic that has the capability of storing and generating its own color. The technique shown here makes the dialog responsible for color of its controls. This is akin to having the dialog responsible for the text that appears in each control. You can do it, by why would you want to?
It makes much more sense to have the control know about itself. The control knows its position, its text, its state; why not have it know about its color? After you change the color, the next thing you are going to want to do is change the font.
Here is how: http://www.codeproject.com/KB/miscctrl/hmxcontrols.aspx
Hi Shane,
Thanks for sharing your ideas!
I totally agree to your points, but only if you have lot of controls to maintain across your product. Then, It’s worth to create a subclassed control.
But what if you want to color just one or two control? In that case, is it wise to subclass and write the entire DrawItem()? even that for each and every type of control that you’re using in UI?
IMHO, the cost-benefit analysis result would be negative, in that case.
Well, keep sharing your thoughts, and thanks for your time!
I would recommend another classic article which explains about subclassing by Chris Mauder, which is much simple and straight forward – http://www.codeproject.com/KB/miscctrl/subclassdemo.aspx
Best Regards,
Jijo.