Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MFC - change text color of a cstatic text control

Tags:

visual-c++

mfc

How do you change the text color of a CStatic text control? Is there a simple way other that using the CDC::SetTextColor?

thanks...

like image 640
Owen Avatar asked Oct 28 '09 11:10

Owen


People also ask

How do I change the color of my static text in MFC?

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.

How do I assign color to text?

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.

How do I change the background color on MFC?

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.


1 Answers

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.

like image 135
djeidot Avatar answered Oct 19 '22 16:10

djeidot