Click here to Skip to main content
Licence CPOL
First Posted 17 Jul 2006
Views 262,571
Downloads 6,012
Bookmarked 391 times

How to write a loading circle animation in .NET?

By | 15 Feb 2007 | Article
A new kind of progress bar for .NET 2.0.

Sample Image - mrg_loadingcircle.jpg

Introduction

When it's time to wait, we are used to seeing the classic blue progress bar. It is everywhere in Windows and many other applications. However, animations are getting more and more popular.

For example, when Firefox loads a page, a small spinning circle appears and shows you that the page is loading. Moreover, Microsoft also uses this kind of animation in the Windows Media Center, Encarta 2006, SQL Server Management Studio Express, etc.

So, why don't we use this concept to show to our users that our application is working and/or loading? Let's begin by the presentation of the component I developed.

Points of interest

Rotation speed

The LoadingCircle uses a timer, and it has two responsibilities: to determine the color of the next spoke, and to redraw the circle at a specified number of milliseconds. When you use the property RotationSpeed, you modify the timer's property named Interval. Higher the value is, slower will be the rotation. The default rotation speed is 80, so at every 80 milliseconds, the circle will be redrawn.

How to draw a spoke?

First of all, we need coordinates for each spoke. We use the function DrawLine of GDI+, which needs two points, the beginning and the end of the line.

So, let's review some simple math notions. In order to draw a perfect circle, you have to know the following trigonometry concept: the cosines of an angle in degrees give us the X and the sine gives us the Y.

The method GetCoordinate computes, for a specified radius and angle, the coordinates of a point.

private PointF GetCoordinate(PointF _objCircleCenter, 
               int _intRadius, double _dblAngle)
{
      PointF objPoint = new PointF();
      double dblAngle = Math.PI * _dblAngle / NUMBER_OF_DEGREES_HALF_CIRCLE;
      objPoint.X = _objCircleCenter.X + _intRadius * (float)Math.Cos(dblAngle);
      objPoint.Y = _objCircleCenter.Y + _intRadius * (float)Math.Sin(dblAngle);
      return objPoint;
}

The method DrawLine uses the coordinates computed by GetCoordinate, and draws a line with the two specified points and a color. Of course, we have to pass to this method the Graphics object of GDI+. As you can see, each line is rounded with the properties StartCap and EndCap of the Pen object.

private void DrawLine(Graphics _objGraphics, PointF _objPointOne, 
                      PointF _objPointTwo, 
                      Color _objColor, int _intLineThickness)
{
      using(Pen objPen = new Pen(new SolidBrush(_objColor), _intLineThickness))
      {
            objPen.StartCap = LineCap.Round;
            objPen.EndCap = LineCap.Round;
            _objGraphics.DrawLine(objPen, _objPointOne, _objPointTwo);
      }
}

How to use this component?

This component is quite easy to use. Once added to your toolbox, drag it to your form, and you are in business! Now, all you have to do is to play with the properties which change the look of the circle. Those are under the category "LoadingCircle" in the Properties panel. Also, you can change the Active property to true or false to see the animation running or not.

The component has the following properties.

  • Color - The color of an inactive spoke.
  • OuterCircleRadius - This property is the radius of the outer circle. The value of this property is supposed to be higher than the InnerCircleRadius value.
  • InnerCircleRadius - This property is the radius of the inner circle.
  • NumberSpoke - This property is the number of spokes.
  • Active - Defines if the component is animated or not. So, if tour treatment is in progress, this property should be set to true, otherwise false.
  • SpokeThickness - Thickness of the line in pixel.
  • RotationSpeed - Animation speed. Lower is the value, faster is the animation.

Conclusion

Finally, I wish you will find this component useful. I had fun writing it, and wish you will use it. Thanks for reading this article.

Revision history

  • June 17, 2006 : Initial release.
  • February 05, 2007 : Better color management (alpha blending)
  • February 10, 2007 : Presets defined in the control (MacOS X, Internet Explorer 7 and FireFox). A new control has been added: LoadingCircleToolStripMenuItem. This control is compatible with the StatusStrip! Supports transparency. Some minors bug corrections.

License

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

About the Author

Martin Gagne

Web Developer

Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow can use this control in Visual Basic 6 Pinmembersixtoja7:04 9 Aug '07  
AnswerRe: How can use this control in Visual Basic 6 PinmemberMartin Gagne7:46 9 Aug '07  
GeneralRe: How can use this control in Visual Basic 6 Pinmembersixtoja8:36 9 Aug '07  
Generalanother animated progress bar PinmemberOwfAdmin9:24 5 Aug '07  
GeneralThreads and loading circle PinmemberCostasAn22:17 17 Jul '07  
GeneralRe: Threads and loading circle PinmemberMartin Gagne4:06 18 Jul '07  
GeneralRe: Threads and loading circle PinmemberCostasAn20:34 18 Jul '07  
Hi Martin,
 
Your suggestion is correct in my opinion but consider this: I develop an application which contains some number of long procedures so I will use loading circle a number of times. I developed a component (which is a splash screen containing the loading circle) which I wish to use in the following way:
 
splashscreen.Show()
longprocedure()
splashscreen.Close()
 
What I want to achieve is the code of the original application to remain as is and just add show() and close() of the splash screen in it. So I desire to add code to the splash screen so the animation will be shown without changing the original code, because the splashscreen component will be given to other programers of the application as is. What I think is if there is some way to "call" the loading circle using a thread or something similar.
 
I tried to add a background worker in the splashscreen which will call a procedure which continuasly calls DoEvents() in a different thread, but this didnt work for some reason I do not understand.
 
BTW loading circle is a very nice and good looking component. If we could find a solution for this problem rest assured that it will be used further not only in the application I am developing but perhaps in more research applications.
 
Thanks and regards,
Costas
GeneralRe: Threads and loading circle PinmemberMartin Gagne5:45 19 Jul '07  
GeneralRe: Threads and loading circle PinmemberAdlibrium7:39 23 Jul '07  
QuestionRe: Threads and loading circle PinmemberJulienV5:17 15 Nov '07  
GeneralRe: Threads and loading circle [modified] Pinmemberbelo300018:44 5 Feb '08  
GeneralThe loading circle application Pinmemberbenson178:53 29 Jun '07  
GeneralRe: The loading circle application PinmemberCostasAn3:32 12 Jul '07  
QuestionIs that you? Pinmembermykel6:25 19 Jun '07  
AnswerRe: Is that you? PinmemberMartin Gagne7:36 19 Jun '07  
GeneralRe: Is that you? Pinmembermykel7:42 19 Jun '07  
AnswerRe: Is that you? Pinmembermerlin9813:32 20 Jun '07  
GeneralRe: Is that you? Pinmembermykel6:39 20 Jun '07  
GeneralRe: Is that you? Pinmembermykel6:48 20 Jun '07  
GeneralRe: Is that you? PinmemberSolidius_Rock13:00 5 Jul '07  
GeneralRe: Is that you? Pinmembermykel4:55 6 Jul '07  
GeneralAdding this animation to a tab control PinmemberMeeul5:40 30 May '07  
GeneralRe: Adding this animation to a tab control PinmemberMartin Gagne6:55 30 May '07  
Questionerrors? Pinmemberpiotr.kolodziej0:20 26 Apr '07  
AnswerRe: errors? Pinmemberpiotr.kolodziej0:27 26 Apr '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 15 Feb 2007
Article Copyright 2006 by Martin Gagne
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid