Click here to Skip to main content
15,885,244 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 66.8K   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;
using EasyProgressBarPacket.Win32;

namespace EasyProgressBarPacket
{
    public class EasyProgressBarSplashForm : Form
    {
        #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 EasyProgressBarSplashForm()
        {
            this.SetStyle(ControlStyles.Selectable, false);
            this.SetStyle(ControlStyles.ResizeRedraw, false);
            this.SetStyle(ControlStyles.FixedWidth | ControlStyles.FixedHeight, true);
            this.DoubleBuffered = true;

            this.ShowIcon = false;
            this.ControlBox = false;
            this.MaximizeBox = false;
            this.ShowInTaskbar = false;
            this.StartPosition = FormStartPosition.Manual;
            this.FormBorderStyle = FormBorderStyle.None;
        }

        #endregion

        #region Destructor

        ~EasyProgressBarSplashForm()
        {
            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;
                }
            }
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;

                // This form has to have the WS_EX_LAYERED extended style.
                cp.ExStyle |= (int)User32.WindowExStyles.WS_EX_LAYERED;
                // Hide from ALT+Tab menu, Turn on WS_EX_TOOLWINDOW style.
                cp.ExStyle |= (int)User32.WindowExStyles.WS_EX_TOOLWINDOW;
                return cp;
            }
        }

        protected override bool ShowWithoutActivation
        {
            // True if the window will not be activated when it is shown; otherwise, false.
            get { return true; }
        }

        #endregion

        #region Virtual Methods

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

            newFrameImage = e.GetActiveFrameImage;
            if (newFrameImage != null)
                GDIWindow((Bitmap)newFrameImage);
        }

        #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);
        }
        
        #endregion

        #region Helper Methods

        private void GDIWindow(Bitmap bmp)
        {
            IntPtr hDC = User32.GetDC(IntPtr.Zero);
            try
            {
                IntPtr hMemDC = User32.CreateCompatibleDC(hDC);
                try
                {
                    IntPtr hBmp = bmp.GetHbitmap(Color.FromArgb(0));
                    try
                    {
                        IntPtr previousBmp = User32.SelectObject(hMemDC, hBmp);
                        try
                        {
                            Point ptDst = new Point(Left, Top);
                            Size size = new Size(bmp.Width, bmp.Height);
                            Point ptSrc = new Point(0, 0);

                            User32.BLENDFUNCTION blend = new User32.BLENDFUNCTION();
                            blend.BlendOp = User32._AC_SRC_OVER;
                            blend.BlendFlags = 0;
                            blend.SourceConstantAlpha = 255;
                            blend.AlphaFormat = User32._AC_SRC_ALPHA;

                            User32.UpdateLayeredWindow(
                                Handle,
                                hDC,
                                ref ptDst,
                                ref size,
                                hMemDC,
                                ref ptSrc,
                                0,
                                ref blend,
                                User32._LWA_ALPHA);
                        }
                        finally
                        {
                            User32.SelectObject(hDC, previousBmp);
                        }
                    }
                    finally
                    {
                        User32.DeleteObject(hBmp);
                    }
                }
                finally
                {
                    User32.DeleteDC(hMemDC);
                }
            }
            finally
            {
                User32.ReleaseDC(IntPtr.Zero, hDC);
            }
        }

        #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