12,746,559 members (30,818 online)
alternative version

#### Stats

500.9K views
460 bookmarked
Posted 17 Jul 2006

, 15 Feb 2007 CPOL
 Rate this:
A new kind of progress bar for .NET 2.0.

## 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,
{
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.

## Share

No Biography provided

## You may also be interested in...

 Pro

 First PrevNext
 Problem with localization - Invalid Property Value Thomas Schoeniger2-Feb-16 0:56 Thomas Schoeniger 2-Feb-16 0:56
 Thank you - Nicely Done HenryT8830-Dec-15 12:07 HenryT88 30-Dec-15 12:07
 Thank you.. praveen kaushal19-Nov-15 20:01 praveen kaushal 19-Nov-15 20:01
 How do I add the component to my toolbox? Member 119718359-Sep-15 8:19 Member 11971835 9-Sep-15 8:19
 Re: How do I add the component to my toolbox? Member 1145586114-Dec-16 22:13 Member 11455861 14-Dec-16 22:13
 PLZ Member 117372329-Jun-15 19:31 Member 11737232 9-Jun-15 19:31
 Transparency problem gorgicze8-Mar-15 8:33 gorgicze 8-Mar-15 8:33
 Re: Transparency problem praveen kaushal19-Nov-15 20:03 praveen kaushal 19-Nov-15 20:03
 Re: Transparency problem gorgicze19-Nov-15 20:51 gorgicze 19-Nov-15 20:51
 NuGet package? Brett Hysuik30-Jan-15 13:17 Brett Hysuik 30-Jan-15 13:17
 My vote of 5 nitin@codeproject27-Nov-14 23:44 nitin@codeproject 27-Nov-14 23:44
 Change it's Size Elaheh Ordoni9-Sep-14 4:09 Elaheh Ordoni 9-Sep-14 4:09
 Re: Change it's Size Elaheh Ordoni9-Sep-14 19:17 Elaheh Ordoni 9-Sep-14 19:17
 Very nice Marcel Breu29-Sep-13 5:29 Marcel Breu 29-Sep-13 5:29
 My vote of 4 ravithejag8-Aug-12 21:11 ravithejag 8-Aug-12 21:11
 Very nice control + a question alex__b11-Jul-12 10:26 alex__b 11-Jul-12 10:26
 Error "Cannot embed interop types from assembly" NewOrdered130-May-12 3:04 NewOrdered1 30-May-12 3:04
 My vote of 5 pcs041425-Apr-12 4:26 pcs0414 25-Apr-12 4:26
 Angles of spokes are shifted anticlockwise - any ideas? Solved! GuyThiebaut25-Mar-12 10:17 GuyThiebaut 25-Mar-12 10:17
 My vote of 5 GuyThiebaut25-Mar-12 7:17 GuyThiebaut 25-Mar-12 7:17
 My vote of 5 manoj kumar choubey18-Feb-12 4:08 manoj kumar choubey 18-Feb-12 4:08
 Does not support threading elhenawyk20062-Feb-12 7:08 elhenawyk2006 2-Feb-12 7:08
 Re: Does not support threading Martin Gagne18-Feb-12 12:23 Martin Gagne 18-Feb-12 12:23
 Re: Does not support threading MichaelMelio27-Jan-16 5:48 MichaelMelio 27-Jan-16 5:48
 Last Visit: 31-Dec-99 19:00     Last Update: 18-Feb-17 22:02 Refresh 1234567 Next »