Double buffering on .NET Compact Framework

馋奶兔 提交于 2019-12-12 01:39:16

问题


I was wondering if someone could explain to me how to double buffer a complete form in .net compact framework. I've found examples of double buffering but I can't seem to get any of these to work.

We created an application with multiple forms. Some of these forms do take a long time to draw on screen which results in flickering. To give you some insight into our application i'm going to explain one of the forms. This form contains a usercontrol , some panels, textboxes and buttons. The usercontrol has panels that have custom drawing (drawing text and images (with transparancy) on screen). And even some of these panels contain other panels that do the same thing. The panels also have custom drawing because we draw text on screen with some effects, etc. Each panel takes time to be drawn, which means if we have 9 panels in a 3x3 grid, they get drawn and show in random order, instead of beeing displayed all at the same time. Same happens with the text drawing, etc. We would like to have everything on the form to be displayed at the same time.

So my question is, can i create a 'super' class that does the double buffering, do the drawing in memory of the complete form?

Can i extend my currect forms from it without having to change anything to my controls, panels, drawing of images, etc?

Thanks


回答1:


I'm not sure about the intricacies of your exact form with all the controls on it, but I've implemented a base DoubleBufferableControl class from which you can extend it to create custom controls that are double buffered. I've created many controls with custom drawing which all use this as a base class.

You can use this class as the base class of your controls which have custom painting to avoid flickering. In your child class be sure to set DoubleBuffered to true in the constructor.

    /// <summary>
/// Implements the functionality for a control that can be double buffered
/// </summary>
public class DoubleBufferableControl : ScrollableControl
{
    public event BufferedPaintEventHandler BufferedPaint;
    private bool doubleBuffered;
    private Bitmap backBuffer;
    private Size oldSize;

    /// <summary>
    /// Gets or sets whether this control will use double buffering
    /// </summary>
    public bool DoubleBuffered
    {
        get
        {
            return doubleBuffered;
        }
        set
        {
            if (value && !doubleBuffered && Width > 0 && Height > 0)
            {
                backBuffer = new Bitmap(Width, Height);
            }
            else if(!value && doubleBuffered)
            {
                backBuffer.Dispose();
                backBuffer = null;
            }

            doubleBuffered = value;
        }
    }

    /// <summary>
    /// Gets the off screen image used for double buffering
    /// </summary>
    public Bitmap BackBuffer
    {
        get
        {
            return backBuffer;
        }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="DoubleBufferableControl"/> class.
    /// </summary>
    public DoubleBufferableControl()
    {
        AutoScroll = false;
        doubleBuffered = DefaultDoubleBuffered;
        oldSize = Size;
    }

    #region Designer
    private bool DefaultDoubleBuffered = false;
    protected virtual bool ShouldSerializeDoubleBuffered()
    {
        return !this.doubleBuffered.Equals(DefaultDoubleBuffered);
    }
    protected void ResetDoubleBuffered()
    {
        DoubleBuffered = DefaultDoubleBuffered;
    }
    #endregion

    /// <summary>
    /// Raises the Paint event
    /// </summary>
    /// <param name="e">A PaintEventArgs that represents event data</param>
    protected override sealed void OnPaint(PaintEventArgs e)
    {
        if (doubleBuffered)
        {
            DoubleBufferedPaintEventArgs pe = new DoubleBufferedPaintEventArgs(CreateGraphics(), e.ClipRectangle);
            OnPaint(pe);
            pe.Graphics.Dispose();
            e.Graphics.DrawImage(backBuffer, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);
            base.OnPaint(e);
        }
        else
        {
            DoubleBufferedPaintEventArgs pe = new DoubleBufferedPaintEventArgs(e.Graphics, e.ClipRectangle);
            OnPaint(pe);
            base.OnPaint(e);
        }
    }

    /// <summary>
    /// Raises the Paint event for child classes that are to be double buffered
    /// </summary>
    /// <param name="e"></param>
    protected virtual void OnPaint(DoubleBufferedPaintEventArgs e)
    {
        if (BufferedPaint != null)
            BufferedPaint(this, e);
    }

    /// <summary>
    /// Paints the background of the control
    /// </summary>
    /// <param name="e">A PaintEventArgs object that contains event data</param>
    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // do not use arg, because can't control back/screen
        Graphics gfx = CreateGraphics();
        gfx.Clear(BackColor);
        gfx.Dispose();
    }

    /// <summary>
    /// Raises the Resize event
    /// </summary>
    /// <param name="e">An EventArgs that represents event data</param>
    protected override void OnResize(System.EventArgs e)
    {
        if (Size != oldSize) // Stupid control gets resized when like anything happens to the parent form
        {
            if (doubleBuffered)
            {
                if (backBuffer != null)
                    backBuffer.Dispose();

                backBuffer = new Bitmap(Width != 0 ? Width : 1, Height != 0 ? Height : 1);
            }
        }
        oldSize = Size;

        base.OnResize(e);
    }

    /// <summary>
    /// Creates the Graphics for the control
    /// </summary>
    /// <param name="backBuffer">True to bypass the buffer and get the control graphics</param>
    /// <returns></returns>
    public virtual Graphics CreateGraphics(bool bypass)
    {
        if(bypass || !doubleBuffered)
            return base.CreateGraphics();
        else
            return Graphics.FromImage(backBuffer);
    }
    public virtual new Graphics CreateGraphics()
    {
        return CreateGraphics(false);
    }
}

and you'll need these:

/// <summary>
/// Provides data for the DoubleBufferedControl.Paint event
/// </summary>
public class DoubleBufferedPaintEventArgs : PaintEventArgs
{
    /// <summary>
    /// Initializes a DoubleBufferedPaintEventArgs
    /// </summary>
    /// <param name="g">The Graphics object to paint to;  If the control is double buffered, the graphics object is for the buffer otherwise the screens graphics is used</param>
    /// <param name="clip">The region in which to paint</param>
    public DoubleBufferedPaintEventArgs(Graphics g, Rectangle clip) : base(g, clip) { }
}
public delegate void BufferedPaintEventHandler(object sender, DoubleBufferedPaintEventArgs args);

I typically inherit from this class, override the OnPaintBackground method and leave its implementation blank. Then, I implement all of the custom drawing in the OnPaint method.



来源:https://stackoverflow.com/questions/24273530/double-buffering-on-net-compact-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!