• 0

[C#] Custom Control Flicker


Question

I have a custom control that draws a gradient and an image. I have overridden the OnPaint event as outlined below and draw the control to an off-screen bitmap before drawing to the screen, which should eliminate flicker. It works pretty well, but there is still a small amount of flicker only on the right edge of the control when I resize the form that it sits on. In the OnResize event of the main form, I simply set the width of the control to the width of the form. Calling the SetStyle function has no effect. It's pretty minor, but I was hoping to completely eliminate it. Any ideas? I can provide the executable if anyone needs to see exactly what is happening.

		protected override void OnPaint(PaintEventArgs e)
		{
			Rectangle rect = new Rectangle(0, 0, ClientSize.Width, ClientSize.Height);
			Color lightBlue = Color.FromArgb(40, 146, 235);
			Color darkBlue = Color.FromArgb(0, 97, 167);

			using (Bitmap bitmapBuffer = new Bitmap(ClientSize.Width, ClientSize.Height))
			using (LinearGradientBrush brush = new LinearGradientBrush(rect, lightBlue, darkBlue, LinearGradientMode.Vertical))
			using (Graphics g = Graphics.FromImage(bitmapBuffer))
			{
				g.FillRectangle(brush, rect);

				if (BackgroundImage != null)
				{
					g.DrawImage(BackgroundImage, 0, 0);
				}

				e.Graphics.DrawImage(bitmapBuffer, 0, 0);
			}
		}

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

i have skim read this post, but have you enabled double buffering on your form(s)?

this should eliminate all flickering on form resize and isn't turned on by default due to it's overhead

edit: reading properly what you describe IS double buffering, but a manual method..

perhaps try turning on automatic db?

myform.SetStyle(ControlStyles.AllPaintingInWMPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true)

Link to comment
Share on other sites

  • 0
i have skim read this post, but have you enabled double buffering on your form(s)?

this should eliminate all flickering on form resize and isn't turned on by default due to it's overhead

Yes, and it has no effect whether on or off (both on the control and on the form). The buffered write to an offscreen bitmap in the OnPaint event is the preferred way of handling it anyway.
Link to comment
Share on other sites

  • 0
Yes, and it has no effect whether on or off (both on the control and on the form). The buffered write to an offscreen bitmap in the OnPaint event is the preferred way of handling it anyway.

hmm.. i'm afraid i only know about this in theory as i do very little image work myself

i'll keep looking and see if i can dig something helpful up though, soz.

Link to comment
Share on other sites

  • 0

Did you also override OnPaintBackground to eliminate background repaints?

protected override void OnPaintBackground(PaintEventArgs pevent)
{
// doing nothing in here!
}

Link to comment
Share on other sites

  • 0
Did you also override OnPaintBackground to eliminate background repaints?
Yes, but thanks for the idea. Controls flicker like crazy if you don't override both OnPaint and OnPaintBackground.

Attached is the program. It's quite small and just a testing ground. Maybe there's something wrong with my machine and nobody else will notice the problem.

To reproduce, simply resize the form horizontally. You should notice a little bit of black tearing near the right edge of the blue control at the top of the form.

Company.zip

Link to comment
Share on other sites

  • 0

Just an update. I told the custom control to inherit from "Control" instead of "UserControl" and it essentially fixed the problem. Weird, but given that UserControls are supposed to be collections of controls, perhaps that makes sense. Thanks for the input everybody.

Edited by boogerjones
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.