How do I prevent background flickering in Windows application

Posted on 2011-10-19
Medium Priority
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 45

Expert Comment

ID: 36998177
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

ID: 37000081
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 45

Expert Comment

ID: 37000337
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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 35

Expert Comment

ID: 37001468
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

mlcarey1959 earned 0 total points
ID: 37002087
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

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This article is about my experience upgrading my consulting machine to Windows 10 Version 1709 (The Fall 2017 Creator Update)
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

850 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