MFC - 更改 cstatic 文本控件的文本颜色

2022-01-12 00:00:00 visual-c++ mfc

如何更改 CStatic 文本控件的文本颜色?除了使用 CDC::SetTextColor 之外,还有其他简单的方法吗?

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

谢谢...

推荐答案

您可以在对话框类中实现 ON_WM_CTLCOLOR,而无需创建新的 CStatic 派生类:

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);
    }
}

请注意,上面的代码设置对话框中所有静态控件的文本.但是你可以使用 pWnd 变量来过滤你想要的控件.

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.

相关文章