c++winapiwtl

Change the background color of a subclassed CStatic control


I am writing my own CStatic subclass right now, and I am a bit stuck on how to change its background color.

From previous experience I am used to reacting to the "CTLCOLORSTATIC" message with the color I want.

However, this is my subclass at the moment:

class LocationPane : public CWindowImpl<LocationPane, CStatic>
{
    DECLARE_WND_CLASS(L"LocationPane");
public:
    BEGIN_MSG_MAP_EX(LocationPane)
        MSG_WM_PAINT(OnPaint)
        MSG_WM_CTLCOLORSTATIC(OnCtlColorStatic);
    END_MSG_MAP()

    LocationPane();
    ~LocationPane();

private:

    HBRUSH OnCtlColorStatic(CDCHandle cd, CWindow wnd);

    //Paint routine
    void OnPaint(CDCHandle dc);
};

I tried to listen for said message, but I am not receiving it at all. I do receive the paint message however, so I can't blame my custom control for not working at all.

Is it a legit solution to try to handle the background color in the paint routine? I don't really like solving it this way, but I am not receiving the other message which I am used to work with.

If additional code is needed, feel free to ask, I will gladly provide you with additional resources.

Thanks in advance.


Solution

  • I think you want to be catching WM_CTLCOLORSTATIC in the parent window that hosts your control. That's typically used for when you want to have a text control have a different background color.

    But if you are going to be overriding WM_PAINT, you might as well let your OnPaint draw the background color.