Sign in to follow this  
Raloth

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

Recommended Posts

Raloth    379
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 this post


Link to post
Share on other sites
Tommy Carlier    142
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 this post


Link to post
Share on other sites
Troj    138
This helps, just put it your form init code.

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this