How do I prevent background flickering in Windows application

Posted on 2011-10-19
Last Modified: 2012-05-12

I have a simple windows application that paints a background image to a rectangle.  Is there a way to paint the background so that it does not flicker?

case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);
      // TODO: Add any drawing code here...
      HBITMAP    hBKBrushBmp;   // small bitmap used to paint main window bkgnd
      RECT       MainWndRect;   // coordinates for the main window
      HBRUSH  hbrBackground;
      hBKBrushBmp = ::LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP19)   );
            MessageBox(NULL,TEXT("Could not load Brush Bmp"),TEXT("Error"),MB_OK);
      hbrBackground = ::CreatePatternBrush(hBKBrushBmp);
      Rectangle(hdc, 2, 2, MainWndRect.right-2, MainWndRect.bottom-2);

      EndPaint(hWnd, &ps);
Question by:mlcarey1959
    LVL 43

    Expert Comment

    Trap the WM_ERASEBKGND message (painting is two step - background and later by another message, foreground via WM_PAINT) and don't do anything there.  That should help a lot.

    Author Comment

    Trapping the WM_ERASEBKGND message does not prevent flickering.  I also tried setting the WNDCLASSEX hbrBackground variable to NULL and it does not work either.
    LVL 43

    Expert Comment

    It should help to reduce flickering.

    Next step you could try a double buffering (perform the drawing in memory and then in one operation copy the finished display to the real display) - have a look at BitBlt in the help files.  There is sample code there.
    LVL 32

    Expert Comment

    to add to above:

    you should paint the bitmap to a compatible memory device context first and then call BitBlt to update the screen dc.


    Accepted Solution

    It was the image.  I was using an 8 x 8 pixel bitmap with a grey background and 16 white squares. This pattern causes flickering no matter what paint method I used.  However, after changing the bitmap, the flickering stopped.  

    LVL 27

    Expert Comment

    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Sometimes a user will call me frantically, explaining that something has gone wrong and they have tried everything (read - they have messed it up more and now need someone to clean up) and it still does no good, can I help them?!  Usually the standa…
    Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
    In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
    Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now