Double Buffering : Double Buffer « 2D Graphics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » 2D Graphics » Double BufferScreenshots 
Double Buffering
Double Buffering

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace GDI_Basics
{
    /// <summary>
    /// Summary description for DoubleBuffering.
    /// </summary>
    public class DoubleBuffering : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.CheckBox chkDoubleBuffer;
        internal System.Windows.Forms.Timer tmrRefresh;
        private System.ComponentModel.IContainer components;

        public DoubleBuffering()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Disposebool disposing )
        {
            ifdisposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Disposedisposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.chkDoubleBuffer = new System.Windows.Forms.CheckBox();
            this.tmrRefresh = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // chkDoubleBuffer
            // 
            this.chkDoubleBuffer.BackColor = System.Drawing.Color.PaleTurquoise;
            this.chkDoubleBuffer.Location = new System.Drawing.Point(88);
            this.chkDoubleBuffer.Name = "chkDoubleBuffer";
            this.chkDoubleBuffer.Size = new System.Drawing.Size(33616);
            this.chkDoubleBuffer.TabIndex = 1;
            this.chkDoubleBuffer.Text = "Use Double Buffering";
            // 
            // tmrRefresh
            // 
            this.tmrRefresh.Interval = 10;
            this.tmrRefresh.Tick += new System.EventHandler(this.tmrRefresh_Tick);
            // 
            // DoubleBuffering
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(514);
            this.ClientSize = new System.Drawing.Size(376294);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.chkDoubleBuffer});
            this.Font = new System.Drawing.Font("Tahoma"8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "DoubleBuffering";
            this.Text = "DoubleBuffering";
            this.Load += new System.EventHandler(this.DoubleBuffering_Load);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.DoubleBuffering_Paint);
            this.ResumeLayout(false);

        }
        #endregion


        private bool isShrinking = false;
        private int extraSize = 0;


        private void tmrRefresh_Tick(object sender, System.EventArgs e)
        {
            // Change the circle dimensions.
            
            if (isShrinking)
            {
                extraSize--;
            }
            else
            {
                extraSize++;
            }
            
            // Change the sizing direction if needed.
            if (extraSize > (this.Width - 150))
            {
                isShrinking = true;
            }
            else if (extraSize < 1)
            {
                isShrinking = false;
            }
            
            // Repaint the form.
            this.Invalidate();

        }

        
        protected override void OnPaintBackground(
            System.Windows.Forms.PaintEventArgs pevent)
        {
            // Do nothing.
        }


        [STAThread]
        static void Main() 
        {
            Application.Run(new DoubleBuffering());
        }

        private void DoubleBuffering_Load(object sender, System.EventArgs e)
        {
            tmrRefresh.Start();
        }

        private void DoubleBuffering_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g;
            Bitmap drawing = null;

            // Check if double buffering is needed, and assign the GDI+ context.
            if (chkDoubleBuffer.Checked)
            {
                drawing = new Bitmap(this.Width, this.Height, e.Graphics);
                g = Graphics.FromImage(drawing);
            }
            else
            {
                g = e.Graphics;
            }

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            // Draw a rectangle.
            Pen drawingPen = new Pen(Color.Black, 10);
            g.FillRectangle(Brushes.PaleTurquoise , new Rectangle(new Point(00),
                this.ClientSize));
            g.DrawEllipse(drawingPen, 505050 + extraSize, 50 + extraSize);

            // If using double buffering, render the final image and dispose of it.
            if (chkDoubleBuffer.Checked)
            {
                e.Graphics.DrawImageUnscaled(drawing, 00);
                g.Dispose();
            }

        }
    }
}



           
       
Related examples in the same category
1.Double buffer drawDouble buffer draw
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.