Windows Mobile double bufferring does not work. Still flickers

290 views Asked by At

I am trying to implement double buffered drawing in a windows mobile application. But I still see the flickering. I am using InvalidateRect() in my rendering code(not shown here), instead of Updating the entire window. The rectangle mentioned in this API is flickering when I update/paint. Please help

            case WM_PAINT:
            {
            hdc = BeginPaint(hWnd, &ps);
            //hdc = GetDC(hWnd);

            HDC newDC = CreateCompatibleDC(hdc);
            HBITMAP hBitmap;
            hBitmap = CreateCompatibleBitmap(hdc,width, height);
                            SelectObject(newDC,hBitmap));
            BitBlt(hdc,0,0,width, height,newDC,0,0,SRCCOPY);

            DeleteDC(newDC);
            DeleteObject(hBitmap);
            EndPaint(hWnd, &ps);
            //ReleaseDC(hWnd,hdc); //Using this causes WM_PAIN fired without any reason.
            }
             break;
3

There are 3 answers

0
avakar On

BeginPaint erases background of the invalid rect, that's probably why you're seing flickering. If the class of the window in question is registered by you, you can set the background brush to GetStockObject(NULL_BRUSH).

As a side note, you can use GetDC/ReleaseDC as long as you revalidate the invalid rectangle by calling ValidateRect.

0
MusiGenesis On

I don't know if this answer is even relevant to C++, but to prevent flickering in .Net you also need to override the control's OnPaintBackground event.

0
noelicus On

Also ensure you return 0 after processing a WM_PAINT. If the function later does a return DefWindowProc() or returns non-0 (i.e. in a dialog) the default window code will also process the WM_PAINT and thus cause a flicker.