MFC - change text color of a cstatic text control

后端 未结 5 1131
野的像风
野的像风 2020-12-29 07:09

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

thanks...

5条回答
  •  清酒与你
    2020-12-29 07:53

    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.

提交回复
热议问题