The Direct2D handlePaintMessage() function problem


#1

when I use JUCE_DIRECT2D, I show an ui without animation, the cpu is about 10%(win10),I think the problem is same as Direct2D, CPU utilization, and continuous WM_PAINT messages described here.
When I wrapper it with BeginPaint and EndPaint, the cpu is down to 0.
Is my version of handlePaintMessage right?

original code

        void handlePaintMessage()
        {
           #if JUCE_DIRECT2D
            if (direct2DContext != nullptr)
            {
                RECT r;
                if (GetUpdateRect (hwnd, &r, false))``
                {
                    direct2DContext->start();
                    direct2DContext->clipToRectangle (rectangleFromRECT (r));
                    handlePaint (*direct2DContext);
                    direct2DContext->end();
                }
            }
        else
       #endif

my version:

void handlePaintMessage()
{
   #if JUCE_DIRECT2D
    if (direct2DContext != nullptr)
    {
        RECT r;

        if (GetUpdateRect (hwnd, &r, false))
        {
			PAINTSTRUCT paintStruct;
			HDC dc = BeginPaint(hwnd, &paintStruct);

            direct2DContext->start();
            direct2DContext->clipToRectangle (rectangleFromRECT (r));
            handlePaint (*direct2DContext);
            direct2DContext->end();

			EndPaint(hwnd, &paintStruct);
        }
    }
    else
   #endif

#2

Not sure about the status of our Direct2D renderer… it’s not very well tested!

Well what you’re suggesting probably sort of works, but it seems like there shouldn’t be any need to use ancient win32 functions like BeginPaint! Perhaps just calling ValidateRect instead? e.g.

        if (direct2DContext != nullptr)
        {
            RECT r;
            if (GetUpdateRect (hwnd, &r, false))``
            {
                direct2DContext->start();
                direct2DContext->clipToRectangle (rectangleFromRECT (r));
                handlePaint (*direct2DContext);
                direct2DContext->end();
                ValidateRect (hwnd, &r);
            }
        }

#3

yeah! ValidateRect also can reduce cpu from 10% to 0%!
Thank you!