Click here to Skip to main content
15,892,298 members
Articles / Mobile Apps

ImageList-enabled Animation Control (ImAC) with Designer Support

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
7 Jul 2005CPOL5 min read 43.2K   524   22  
An article on writing an ImageList-based animation control with designer support.
using System;
// Copyright (C) 2005 by Henry Tan
// All rights reserved
//
// This is free software for non-profit usage. Any commercial use of the
// code must be through a licensing agreement between Visual Obeje and
// the parties of interest.

// This code may be used in compiled form in any way you desire. This  
// file may be redistributed unmodified by any means PROVIDING it is   
// not sold for profit without the authors written consent, and   
// providing that this notice and the authors name and all copyright   
// notices remains intact. If the source code in this file is used in   
// any  commercial application then a statement along the lines of   
// "Portions Copyright � 2005 Henry Tan" must be included in   
// the startup banner, "About" box or printed documentation. An email   
// letting me know that you are using it would be nice as well. That's   
// not much to ask considering the amount of work that went into this.  
//  
// No warrantee of any kind, expressed or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc., and
// I'll try to keep a version up to date.  I can be reached as follows:
// henryws@it.uts.edu.au
/////////////////////////////////////////////////////////////////////////////
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace HT.CustomControl
{
	/// <summary>
	/// Summary description for AnimationCtrl.
	/// </summary>
	public class AnimationCtrl : System.Windows.Forms.Control
	{
		#region fields
		protected Bitmap	m_bmp;          // offscreen bitmap
		protected Graphics	m_graphics;
		protected int		iloop;
		protected int		nLoop;
		protected bool		bStarted;
		protected int		ci;	            // index that point to current image in imglist
		protected Timer		tmrAnm = null;	// timer controlling animation
		private ImageList   imglist = null; 
		#endregion

		#region properties
#if NETCFDESIGNTIME
		[System.ComponentModel.Browsable(true)]
		[System.ComponentModel.Category("Behavior")]
		[System.ComponentModel.DefaultValue(-1)]
		[System.ComponentModel.Description("Determines the number of loops, -1 will loop forever")]
#endif
		public int Loop // number of looping
		{
			get
			{
				return nLoop;
			}

			set
			{
				nLoop = value;
			}
		}

#if NETCFDESIGNTIME
		[System.ComponentModel.Browsable(true)]
		[System.ComponentModel.Category("Behavior")]
		[System.ComponentModel.DefaultValue(1000)]
		[System.ComponentModel.Description("The animation interval in miliseconds")]
#endif
		public int Interval // in miliseconds
		{
			get
			{
				return tmrAnm.Interval;
			}

			set
			{
				tmrAnm.Interval = value;
			}
		}

#if NETCFDESIGNTIME
		[System.ComponentModel.Browsable(true)]
		[System.ComponentModel.Category("Appearance")]
		[System.ComponentModel.DefaultValue(null)]
		[System.ComponentModel.Description("The list of images to be animated")]
#endif
		public ImageList ImageList
		{
			get
			{
				return imglist;
			}

			set
			{
				imglist = value;
			}
		}
		#endregion
		
		
		#region initialization
		// default constructor
		public AnimationCtrl()
		{
			InitializeComponents();
		}

		private void InitializeComponents()
		{
			///
			/// Image List
			/// List of images to be animated
			imglist = new ImageList();

			/// Timer
			/// Binding the timer.Tick event to the handler
			tmrAnm = new Timer();
			tmrAnm.Interval = 1000;
			tmrAnm.Tick += new EventHandler(tmrAnm_Tick);

			///
			/// ci
			///
			ci = 0;

			///
			/// flag
			/// 
			bStarted = false;

			nLoop = -1;
			iloop = 0;
		}

		#endregion

		#region methods
		/// <summary>
		/// Create offsceeen bitmap. This bitmap is used for double-buffering
		/// to prevent flashing.
		/// </summary>
		private void CreateMemoryBitmap()
		{
			if (m_bmp == null || m_bmp.Width != this.Width || m_bmp.Height != this.Height)
			{
				// memory bitmap
	
				m_bmp = new Bitmap(this.Width, this.Height);
				m_graphics = Graphics.FromImage(m_bmp);	
			}
		}

		public void Reset()
		{
			Stop();

			ci = 0;
		}

		public void Start()
		{
			if(!bStarted)
			{
				bStarted = true;
				tmrAnm.Enabled = true;
			}
		}

		public void Stop()
		{
			bStarted = false;
			tmrAnm.Enabled = false;
		}

		
		#endregion

		#region event handler
		private void tmrAnm_Tick(object sender, EventArgs e)
		{	
			Invalidate();
		}
		#endregion

		#region overridden methods
		protected override void OnPaint(PaintEventArgs e)
		{
			// draw to memory bitmap
			CreateMemoryBitmap();

			// init background
			m_graphics.Clear(this.BackColor);

			if(imglist.Images.Count > 0)
			{
				Bitmap bmp = new Bitmap(imglist.Images[ci]);
				m_graphics.DrawImage(bmp, 0, 0);

				if(bStarted)
				{
					if(nLoop == -1)
					{
						ci = (ci + 1) % imglist.Images.Count;
					}
					else
					{
						if(iloop >= nLoop)
						{
							Stop();
							iloop = 0;
						}
						else
						{
							iloop++;
						}

						ci = (ci + 1) % imglist.Images.Count;		
					}
				}

				
			}

			// blit memory bitmap to screen
			e.Graphics.DrawImage(m_bmp, 0, 0);
		}

		protected override void OnPaintBackground(PaintEventArgs e)
		{
			// don't pass to base since we paint everything, avoid flashing
		}

		protected override void OnResize(EventArgs e)
		{
			base.OnResize (e);
			this.Invalidate();
		}
		#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
United States United States
Henry Tan was born in a small town, Sukabumi, Indonesia, on December 7th, 1979. He obtained his Bachelor of Computer System Engineering with first class honour from La Trobe University, VIC, Australia in 2003. During his undergraduate study, he was nominated as the most outstanding Honours Student in Computer Science. Additionally, he was the holder of 2003 ACS Student Award. After he finished his Honour year at La Trobe University, on August 2003, he continued his study pursuing his doctorate degree at UTS under supervision Prof. Tharam S. Dillon. He obtained his PhD on March 2008. His research interests include Data Mining, Computer Graphics, Game Programming, Neural Network, AI, and Software Development. On January 2006 he took the job offer from Microsoft Redmond, USA as a Software Design Engineer (SDE).


What he like most? Programming!Math!Physisc!Computer Graphics!Playing Game!

What programming language?
C/C++ but started to love C#.

Comments and Discussions