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

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
This helps, just put it your form init code.

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628301
• Total Posts
2981906

• 9
• 11
• 11
• 10
• 10