[.net] Adding to the Panel.Paint event and flickering

Started by
1 comment, last by Troj 19 years, 8 months ago
I have a panel that I'm rendering to using D3D. Before I was rendering to it through the main form's Paint event. This worked perfectly. Now I have added another panel that I want to render to, so to be able to let them render at different times I changed it to the panel's Paint event. Unfortunately, this leads to an unbearable flickering effect. I understand that it's happening because the panel first renders its usual stuff and then renders my D3D on top of it, but I don't know how to stop it. I also don't understand why this wasn't happening when implementing the main form's Paint. When using the direct Win32 message queue I would stop this by intercepting the Paint message, calling BeginPaint, rendering, and then calling EndPaint. Is there something similar I can do with .NET, or will I have to interact with the message procedure directly?
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
Advertisement
Don't use the Paint-event like this: panel.Paint += ...
Instead of using a Panel, create a custom control class and override the OnPaint-method. To reduce flickering, also override the OnPaintBackground-method.
using System.Drawing;using System.Windows.Forms;public class RenderControl : Control{    protected override void OnPaint(PaintEventArgs e)    {        // render-code goes here        base.OnPaint(e);    }    protected override void OnPaintBackground(PaintEventArgs e)    {        // do nothing here: doesn't paint background => no flickering    }}

Then use the RenderControl in your form, like any other control.
This helps, just put it your form init code.

this.Setstyle(Controlstyles.DoubleBuffer | Controlstyles.AllPaintingInWmPaint | Controlstyles.UserPaint, true);

This topic is closed to new replies.

Advertisement