Search code examples
winapiatlwtl

Redirect a WM_PAINT to default handler, replacing the DC?


Given a class deriving from, say, CEdit, is it possible to intercept the WM_PAINT message, calling the base class message with a different CDC?

    class CEditNew : public CWindowImpl<CEditNew, CEdit>
    {
    public:

        BEGIN_MSG_MAP(CEditNew)
            MESSAGE_HANDLER(WM_PAINT, OnPaint)  
        END_MSG_MAP()

        LRESULT OnPaint(UINT, WPARAM, LPARAM, BOOL &)
        {           
            return 0;
        }
    };

I can intercept the WM_PAINT of the CEdit by calling SubclassWindow but I'd really like to then call the base class original paint handler.

For context the parent window has its own compatibledc (it's using translucency) and I need to allow child controls to paint themselves when they need to by giving them this dc.


Solution

  • The target HDC is not passed in the WM_PAINT message itself, it is obtained from BeginPaint(). You have no control over which HDC it returns.

    The correct way to ask an HWND to paint itself to a specific HDC is to use the PrintWindow() function, or send it a WM_PRINT message directly.