Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / C#

Drawing multiple layers without flicker

Rate me:
Please Sign up or sign in to vote.
4.87/5 (11 votes)
7 Jun 2010CPOL4 min read 58.9K   3.5K   48  
Improve drawing speed using layers in .NET.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestBufferDrawing
{
    

    public partial class FrmTest : Form
    {
        static System.Random gen = new System.Random();
        public FrmTest()
        {
            InitializeComponent();
        }

        private void FrmTest_Load(object sender, EventArgs e)
        {
            TmrBackground.Start();
            TmrRectangles.Start();
            TmrCircles.Start();
            usrcontrol.uselayers = false;
        }
        private void FrmTest_Resize(object sender, EventArgs e)
        {
            usrcontrol.Width = this.Width - usrcontrol.Left * 3;
            usrcontrol.Height = this.Height - usrcontrol.Top * 3;
        }
        private void TmrCircles_Tick(object sender, EventArgs e)
        {
            if (usrcontrol.dotwidth == 7)
                usrcontrol.dotwidth = 1;
            else
                usrcontrol.dotwidth++;
            LblTimePaint.Text = (usrcontrol.timeused.TotalMilliseconds / usrcontrol.paintiterations).ToString();
        }

        private void TmrBackground_Tick(object sender, EventArgs e)
        {
            int R = gen.Next(255);
            int G = gen.Next(255);
            int B = gen.Next(255);
            usrcontrol.ColorBackGround = Color.FromArgb(R, G, B);
        }

        private void TmrRectangles_Tick(object sender, EventArgs e)
        {
            int R = gen.Next(255);
            int G = gen.Next(255);
            int B = gen.Next(255);
            usrcontrol.ColorRectangle = Color.FromArgb(R, G, B);
        }

        private void ChkMultipleLayers_CheckedChanged(object sender, EventArgs e)
        {
            usrcontrol.uselayers = ChkMultipleLayers.Checked;
            usrcontrol.InitIter();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Argentina Argentina
System developer from Argentina.

Programmed in VB 5,6,.NET, C#, Java, PL-SQL, Transac-SQL, C, C++ and even some "calculator" language.

Love to build small, useful applications.
Usually building big and complicated apps based on solid, reliable components.

Hobbies: reading, photography, chess, paddle, running.

Comments and Discussions