none
Flicker during OnPaint when tracking mouse movement RRS feed

  • Question

  • I have a dialog that I have overridden the OnPaint to draw my own graphics in a CStatic control. I am also capturing mouse movement to allow the user to move items in the image. Everything works correctly, except that I get some flickering as the mouse is moved and I update the data and issue a WM_PAINT message to draw the item at the new location. The flickering is just a white bar that moves from top toward the bottom of the image, but doesnt happen all the time. It also is not visible through the whole image, I may see it move a little at the top of the image and then again a little at the bottom of the image. Sometime I see it through the whole image. Any ideas as to why this is happening?
    Tuesday, September 26, 2006 8:24 PM

Answers

  • You may need use MemoryDC for drawing. Please see the following article Flicker Free Drawing In MFC

    Wednesday, September 27, 2006 4:12 AM
  • In my opinion drawing of a control consists on two parts: painting of background as a response to WM_ERASEBKGND message, and drawing the items as a response to WM_PAINT message. You probably provide a handler for WM_PAINT message only, in which you actually draw the entire control.

     

    I think the flickering is caused by the original background drawn in WM_ERASEBKGND handler. You probably do not need this, since you override it in your WM_PAINT handler.

     

    You should either provide your handler for WM_ERASEBKGND, in which you have to draw the right background, or disable the default background and do the drawing entirely in your WM_PAINT handler.

     

    In order to disable the default background, simply do nothing and return TRUE:

     

    BOOL MyStatic::OnEraseBkgnd(CDC* pDC)

    {

        return TRUE;

    }

     

    In this case be sure you draw all parts of the control in your OnPaint.

     

    The next improvement is using of intermediate bitmap image as you were suggested before by Sarath.

     

    I hope this helps.

     

    Wednesday, September 27, 2006 7:03 AM

All replies

  • You may need use MemoryDC for drawing. Please see the following article Flicker Free Drawing In MFC

    Wednesday, September 27, 2006 4:12 AM
  • In my opinion drawing of a control consists on two parts: painting of background as a response to WM_ERASEBKGND message, and drawing the items as a response to WM_PAINT message. You probably provide a handler for WM_PAINT message only, in which you actually draw the entire control.

     

    I think the flickering is caused by the original background drawn in WM_ERASEBKGND handler. You probably do not need this, since you override it in your WM_PAINT handler.

     

    You should either provide your handler for WM_ERASEBKGND, in which you have to draw the right background, or disable the default background and do the drawing entirely in your WM_PAINT handler.

     

    In order to disable the default background, simply do nothing and return TRUE:

     

    BOOL MyStatic::OnEraseBkgnd(CDC* pDC)

    {

        return TRUE;

    }

     

    In this case be sure you draw all parts of the control in your OnPaint.

     

    The next improvement is using of intermediate bitmap image as you were suggested before by Sarath.

     

    I hope this helps.

     

    Wednesday, September 27, 2006 7:03 AM