Mo Code
Mo Code

Reputation: 117

C++ MFC Changing Background color of radio box

I'm trying to change the background color of three radio boxes in my MFC application but they seem to remain the default colors no matter what.

Here is my latest attempt: The IDC_LIKE, IDC_FOLLOW, and IDC_COMMENT are the identifiers of the three radio boxes I'm trying to change the colors of. I've even tried changing the CTLCOLOR_STATIC fields but that doesn't work either.

How can I fix?

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

Upvotes: 3

Views: 2664

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31669

SetTextColor and SetBkColor don't affect button's text color and background color. You have to return a brush. You already have m_brush, just use that for dialog and the controls:

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

Where m_brush is created as CreateSolidBrush(RGB(136, 217, 242))...

To handle static controls as well, it's enough to set the background mode to transparent:

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

Or you can use background color + opaque background for static and edit controls:

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

Upvotes: 2

Related Questions