2

I have an MFC dialog-based program with multiple elements. I'm developing on Windows 7 using VS2010 professional with SP1. I want to change the background color of some of the slider elements (from the CSliderCtrl class). The only thing I've found is to try to override CSliderCtrl's OnCtlColor function. I did this via the following:

class MySlider : public CSliderCtrl
{
public:
    MySlider(UINT r, UINT g, UINT b){R=r;G=g;B=b;}
    virtual ~MySlider(){}

    UINT R;
    UINT G;
    UINT B;

    HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        pDC->SetBkColor(RGB(R, G, B));

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

Then I replaced all of the CSliderCtrl elements with MySlider elements, and passed in the desired background rgb values to the constructor. However, this did not end up working.

Can someone help me figure out how to properly set the background color of slider elements? (or any other elements for that matter)

user3330644
  • 413
  • 1
  • 6
  • 17
  • Related? http://stackoverflow.com/q/12006168/3747990, IIRC, the `CSliderCtrl::OnCtlColor` should be called before the `SetBkColor`. – Niall Jul 28 '14 at 20:01

2 Answers2

0

Override OnPaint and draw a solid rectangle

void MySlider::OnPaint() 
{
    CPaintDC dc(this); // device context for painting

    RECT rect ;

    CRect rectButton;
    this->GetWindowRect(&rectButton);

    COLORREF cr = RGB(60,180,80)
    dc.FillSolidRect(&rect, cr); // Background color


        // Any other drawing
}
SingleStepper
  • 348
  • 1
  • 10
0

Create a brush with the background color and return that HBRUSH to get the color change.

HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    CSliderCtrl::OnCtlColor(pDC, pWnd, nCtlColor);

    pDC->SetBkColor(RGB(R, G, B));
    static CBrush br(RGB(R, G, B));

    return (HBRUSH)br;
}
HariDev
  • 508
  • 10
  • 28