MFC - change text color of a cstatic text control

后端 未结 5 1116
野的像风
野的像风 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条回答
  •  旧时难觅i
    2020-12-29 07:55

    unfortunately you won't find a SetTextColor method in the CStatic class. If you want to change the text color of a CStatic you will have to code a bit more.

    In my opinion the best way is creating your own CStatic-derived class (CMyStatic) and there cacth the ON_WM_CTLCOLOR_REFLECT notification message.

    BEGIN_MESSAGE_MAP(CMyStatic, CStatic)
        //{{AFX_MSG_MAP(CMyStatic)
        ON_WM_CTLCOLOR_REFLECT()
        //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    HBRUSH CColorStatic::CtlColor(CDC* pDC, UINT nCtlColor) 
    {
        pDC->SetTextColor(RGB(255,0,0)); 
    
        return (HBRUSH)GetStockObject(NULL_BRUSH);  
    }
    

    Obviously you can use a member variable and a setter method to replace the red color (RGB(255,0,0)).

    Regards.

提交回复
热议问题