更改静态文本的颜色C ++

时间:2018-11-02 07:08:03

标签: c++ visual-studio window

我如下通过使用表单设计器编辑.rc文件为MFC类创建了对话框表单, enter image description here

我想更改上方 Namodaya Balaarachchi 文本字段的颜色。 但是在以下属性窗口中没有任何属性。

enter image description here

有人可以帮助我更改上述静态文本的颜色吗?

1 个答案:

答案 0 :(得分:2)

我正在使用Visual Studio 2017并创建一个基于对话框的MFC项目。之后,选择“资源视图”并双击以打开对话框。右键单击并选择“类向导”,在“消息”中,双击WM_CTLCOLOR,然后选择“ OnCtlColor”和“编辑代码”,添加以下“开关”部分。

HBRUSH CMFCApplication1Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

    // TODO:  Change any attributes of the DC here
    switch (nCtlColor)
    {
    case CTLCOLOR_STATIC:
        pDC->SetTextColor(RGB(255, 0, 0));
        return (HBRUSH)GetStockObject(NULL_BRUSH);
    }
    // TODO:  Return a different brush if the default is not desired
    return hbr;
}

运行F5,文本变为红色:

enter image description here

相关问题