devxlogo

OnPaint Flickering

OnPaint Flickering

Question:
I am a high school student using MS VC++ version 5.0 for my school science project. I am loading bitmaps with the StretchBlt function in the OnPaint function to simulate an object moving. It works, but the object, and any backround Bitmap flicker. How can I avoid this?

Answer:
There are many places to look for the cause of flickering. The first place to look is within your own paint code. If all you are doing is calling StretchBlt, then it is probably a safe assumption that you are not the cause of the flicker.

Another place to look is at the processing of the WM_ERASEBKGND message. Windows sends this message before WM_PAINT. If a window is created with the CS_VREDRAW and CS_HREDRAW style bytes, the window is automatically cleared before painting.

To override this behavior, you can override OnEraseBkgnd, or you can override PreCreateWindow to change the style bits used to create your window like this:

BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs) {   // Here we create our own class which is registered through MFC   // If we also create our window with no background brush,   // we can avoid the window being cleared prior to OnPaint   if (cs.lpszClass == NULL)      cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS);   return TRUE;}

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist