Sergio Flores
Sergio Flores

Reputation: 5427

Change the item background color of a combobox in MFC?

I want to change the background color of an item of a combobox when I use the method AddString.

Upvotes: 2

Views: 7033

Answers (1)

demoncodemonkey
demoncodemonkey

Reputation: 11957

Derive a class from CComboBox and override OnCtlColor. In OnCtlColor when the nCtlColor parameter is CTLCOLOR_LISTBOX, call the DC::SetBkColor method.

Maybe something like this:

HBRUSH CMyComboBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    if (nCtlColor == CTLCOLOR_LISTBOX)
        pDC->SetBkColor(RGB(255, 0, 0));

    return CComboBox::OnCtlColor(pDC, pWnd, nCtlColor);
}

Inspiration taken from here and here.

Upvotes: 4

Related Questions