How can I keep MyControl_Paint() from flickering?

  • Thread starter Thread starter Axel Dahmen
  • Start date Start date
A

Axel Dahmen

Hi,

I've created an owner drawn control providing an animation. Within MyControl_Paint() I'm first drawing the background, then I add necessary bars and circles. A timer controls calls Invalidate() every 100 milliseconds.

I notice a strong flickering during the paint cycle. Is it possible to easily get rid of this? Can I defer updating the screen until MyControl_Paint() ends? Or do I need to create an off-screen bitmap manually?

Your help is quite appreciated!

TIA,
Axel Dahmen
 
Hi Axel,

You have two options to reduce the flicker problem in your application.

Option 1: Draw Images Off-Screen

You can reduce the flicker when drawing large images by using a Graphics
object not associated with the form to create the image off-screen. Then
draw the image on the screen using a Graphics object of the form.

For more information on how to do this, please refer to the following MSDN
Document:

'How to: Draw Images Off-Screen'
http://msdn2.microsoft.com/en-us/library/ms172506.aspx

Option 2: Double Buffering

Double buffering uses a memory buffer to address the flicker problems
associated with multiple paint operations. When double buffering is
enabled, all paint operations are first rendered to a memory buffer instead
of the drawing surface on the screen. After all paint operations are
completed, the memory buffer is copied directly to the drawing surface
associated with it. Because only one graphics operation is performed on the
screen, the image flickering associated with complex painting operations is
eliminated.

For more information on double buffering, please refer to the following
MSDN Documents:

'Double Buffered Graphics'
https://msdn2.microsoft.com/en-us/library/b367a457.aspx

'How to: Manually Manage Buffered Graphics'
https://msdn2.microsoft.com/en-us/library/tdk2485d.aspx

'How to: Manually Render Buffered Graphics'
https://msdn2.microsoft.com/en-us/library/ka0yazs1.aspx

Hope this helps.
If you have any question, please feel free to me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks, Linda, for trying to help.

Unfortunately I'm running this particular project on VStudio 2002/.NET 1.0, which doesn't appear to have a DoubleBuffered property or any other double buffering support. Or does it?

Regards,
Axel Dahmen
 
Hi Axel,

Yes, you're right. Double buffering is only supported from .NET 2.0.

You may have a try using the option 1 I suggested in my first reply, i.e.
draw images off-screen to sovle your problem.

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hi Linda,

I've now tried your suggestion drawing an image off-screen and copying it back to the control's Paint() event Graphics object.

Unfortunately the resulting animation still flickers. It seems that the control's intrinsic WM_ERASEBKGND message causes the flickering. Can I keep the control from erasing its background before raising the Paint() event? I could perform an initial FillRectangle() myself within the Paint() event then.

Your help is quite appreciated.

Best regards,
www.axeldahmen.de
Axel Dahmen
 
Hi Axel,

Thank you for your feedback!
It seems that the control's intrinsic WM_ERASEBKGND message causes the
flickering.

Yes, it may be the reason.
Can I keep the control from erasing its background before raising the
Paint() event?

Yes. You can do it without any problem.

If the problem is still not solved, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
You should override the OnPaintBackground to do nothing (don't call the
base class).

You can set double buffering on using the SetStyle method and
ControlStyles.DoubleBuffer.

If you need to you can use manual double buffering.

Se my site for details of all these techniques.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Back
Top