How do you change the text color of a CStatic text control? Is there a simple way other that using the CDC::SetTextColor?
thanks...
I solved this as follows: First, call the base class version of OnCtlColor() (no matter what) and store the returned HBRUSH in a variable. Next, if appropriate, call pDC->SetTextColor() . Finally, return the brush from the first step.
Text color using Hex color codes The most common way of coloring HTML text is by using hexadecimal color codes (Hex code for short). Simply add a style attribute to the text element you want to color – a paragraph in the example below – and use the color property with your Hex code.
1) Added "CBrush m_brush" to the header public. 2) Added "m_brush. CreateSolidBrush(RGB(255, 0, 0))" on init. 3) Added OnCtlColor in Dlg.
You can implement ON_WM_CTLCOLOR
in your dialog class, without having to create a new CStatic-derived class:
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
//{{AFX_MSG_MAP(CMyDialog)
ON_WM_CTLCOLOR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd *pWnd, UINT nCtlColor)
{
switch (nCtlColor)
{
case CTLCOLOR_STATIC:
pDC->SetTextColor(RGB(255, 0, 0));
return (HBRUSH)GetStockObject(NULL_BRUSH);
default:
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
}
Notice that the code above sets the text of all static controls in the dialog. But you can use the pWnd
variable to filter the controls you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With