MFC-更改静态文本控件的文本颜色 [英] MFC - change text color of a cstatic text control

查看:335
本文介绍了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.

这篇关于MFC-更改静态文本控件的文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆