• Advertisement
Sign in to follow this  

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

This topic is 4915 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement