views:

291

answers:

2

Hi all, I have a background image stored in the database so instead of setting BackgroundImage property of the form I handle OnPaint event to draw the image on the background for each form. The problem is that each control on the form invalidates parent control - the form, so the OnPaint fires multiple times (12x). Even though the image is in memory the form shortly flickers. I need to get rid of this effect. I can't see a way how to determine which control is the last to cause the parent form to repaint so I can apply the background just once. Any ideas?

    protected override void OnPaint(PaintEventArgs e)
    {
        if (Program.AppManager.AppBackgroundImage != null && !this.EH_BackImageNotApplicable)
        {
            e.Graphics.DrawImage(Program.AppManager.AppBackgroundImage, this.ClientRectangle);
        }
    }
A: 

Make sure the "DoubleBuffered" property on the form is set to true.

David
It helped a bit but still not great. I didn't even know there is a thing like this. Thanks for that.
David
A: 

Try this:

SetStyle(ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);
// OR
DoubleBufferred = true; // sets both flags

If that doesn't help, you can manage your own double-buffering. Look for articles like this: http://www.switchonthecode.com/tutorials/winforms-tutorial-manage-your-own-double-buffering

John Fisher