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

EasyProgressBarPacket

Rate me:
Please Sign up or sign in to vote.
4.84/5 (42 votes)
18 Nov 2011CPOL4 min read 67.3K   6.9K   105  
A few progressbar examples with clone support.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using EasyProgressBarPacket.Components;

namespace EasyProgressBarPacket
{
    public class AnimatedEasyProgressControl : Control
    {
        #region Event

        /// <summary>
        /// Represents an event which occurs when the index of the active frame changed.
        /// </summary>
        [Description("Represents an event which occurs when the index of the active frame changed")]
        public event EventHandler<ReportTaskProgressEventArgs> FrameChanged;

        #endregion

        #region Instance Members

        private Image newFrameImage = null;
        private AniEasyProgressTaskManager easyProgressTaskManager = null;

        #endregion

        #region Constructor

        public AnimatedEasyProgressControl()
        {
            this.SetStyle(ControlStyles.Selectable, false);
            this.SetStyle(ControlStyles.ResizeRedraw, false);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true);

            // Object Initializer
            this.Size = new Size()
            {
                Width = 100,
                Height = 50
            };
        }

        #endregion

        #region Destructor

        ~AnimatedEasyProgressControl()
        {
            GC.SuppressFinalize(this);
        }

        #endregion

        #region Property

        /// <summary>
        /// Gets or Sets the task manager for painting this control.
        /// </summary>
        [Description("Gets or Sets the task manager for painting this control")]
        public AniEasyProgressTaskManager EasyProgressTaskManager
        {
            get { return easyProgressTaskManager; }
            set
            {
                try
                {
                    if (!value.Equals(easyProgressTaskManager))
                    {
                        if (easyProgressTaskManager != null)
                            easyProgressTaskManager.TaskProgress -= OnFrameChanged;

                        easyProgressTaskManager = value;
                        easyProgressTaskManager.TaskProgress += new EventHandler<ReportTaskProgressEventArgs>(OnFrameChanged);
                    }
                }
                catch (NullReferenceException)
                {
                    if (easyProgressTaskManager != null)
                        easyProgressTaskManager.TaskProgress -= OnFrameChanged;

                    easyProgressTaskManager = null;
                }
            }
        }

        #endregion

        #region Virtual Methods

        protected virtual void OnFrameChanged(object sender, ReportTaskProgressEventArgs e)
        {
            if (FrameChanged != null)
                FrameChanged(this, e);
            
            newFrameImage = e.GetActiveFrameImage;

            this.Invalidate();
            this.Update();
        }

        #endregion

        #region Override Methods
        
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                EasyProgressTaskManager = null;
            }

            base.Dispose(disposing);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (Visible)
            {
                try
                {
                    if (newFrameImage != null)
                    {
                        Rectangle rct = this.ClientRectangle;
                        e.Graphics.DrawImageUnscaled(newFrameImage, rct);
                    }
                }
                catch { ;}
            }
        }

        #endregion
    }
}

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 (Senior) ARELTEK
Turkey Turkey
Since 1998...

MCPD - Enterprise Application Developer

“Hesaplı hareket ettiğini zanneden ve onunla iftihar eyliyen dar kafalar; kurtulmağa, yükselmeğe elverişli hiç bir eser vücüda getirmezler. Kurtuluş ve yükselişi, ancak varlığına dayanan ve mülkü milletin gizli kapalı hazinelerini verimli hale getirmesini bilen, şahsi menfaatini millet menfaati uğruna feda eden, ruhu idealist, dimağı realist şahsiyetlerde aramalıdır.”

Nuri Demirağ, 1947

Comments and Discussions