C ++ MFC更改单选框的背景颜色

时间:2016-05-27 02:20:05

标签: c++ mfc radio

我正在尝试更改MFC应用程序中三个无线电盒的背景颜色,但无论如何它们似乎仍然是默认颜色。

这是我最近的尝试: IDC_LIKE,IDC_FOLLOW和IDC_COMMENT是我试图改变颜色的三个无线电盒的标识符。 我甚至尝试过更改CTLCOLOR_STATIC字段,但这也不起作用。

我该如何解决?

 // CCompleteControlDlg message handlers
HBRUSH CCompleteControlDlg::OnCtlColor(CDC * pDC, CWnd * pWnd, UINT nCtlColor)
{
if (pWnd->GetDlgCtrlID() == IDC_LIKE || pWnd->GetDlgCtrlID() == IDC_FOLLOW || pWnd->GetDlgCtrlID() == IDC_COMMENT) {
    pDC->SetTextColor(RGB(0, 0, 0));
    pDC->SetBkColor(RGB(136, 217, 242));
    pDC->SetBkMode(TRANSPARENT);
    return (HBRUSH)GetStockObject(NULL_BRUSH);
}
else {
    switch (nCtlColor) {
    case CTLCOLOR_STATIC:
        pDC->SetTextColor(RGB(0, 0, 0));
        pDC->SetBkColor(RGB(136, 217, 242));
        pDC->SetBkMode(TRANSPARENT);
        return (HBRUSH)GetStockObject(NULL_BRUSH);

    case CTLCOLOR_DLG: return m_brush;

    case CTLCOLOR_BTN:
        pDC->SetTextColor(RGB(255, 255, 255));
        pDC->SetBkColor(RGB(136, 217, 242));
        return (HBRUSH)GetStockObject(NULL_BRUSH);

    default:
        return CCompleteControlDlg::OnCtlColor(pDC, pWnd, nCtlColor);
    }
}
}

1 个答案:

答案 0 :(得分:2)

SetTextColorSetBkColor不会影响按钮的文字颜色和背景颜色。你必须返回画笔。您已经拥有m_brush,只需将其用于对话框和控件:

HBRUSH CCompleteControlDlg::OnCtlColor(CDC * pDC, CWnd * pWnd, UINT nCtlColor)
{
    ...
    return m_brush;
}

m_brush创建为CreateSolidBrush(RGB(136, 217, 242))...

要处理静态控件,只需将背景模式设置为透明:

HBRUSH CCompleteControlDlg::OnCtlColor(CDC * pDC, CWnd * pWnd, UINT nCtlColor)
{
    pDC->SetTextColor(RGB(0, 0, 0));
    pDC->SetBkMode(TRANSPARENT);
    return m_brush;
}

或者您可以使用背景颜色+不透明背景进行静态和编辑控件:

pDC->SetTextColor(RGB(0, 0, 0));
pDC->SetBkColor(RGB(136, 217, 242));
pDC->SetBkMode(OPAQUE);
return m_brush;