Click here to Skip to main content
15,893,588 members
Articles / Desktop Programming / Windows Forms
Article

Some C# Controls - SplashScreen, PixelProgressBar, PieGraph

Rate me:
Please Sign up or sign in to vote.
3.03/5 (16 votes)
13 Feb 20074 min read 74.6K   2.8K   55   8
A few easy to use controls, written in C#

Progress Bar and Pie Chart

SplashScreen

Introduction

OK! I've written a couple of controls in C#, and I'm sure the whole world is interested in them! The first one is a splash screen that only takes 2 lines of code to use. The second is a progress bar that draws an image instead of rectangles, which I think looks nice. The third is a pie chart control, which I actually needed for something I'm doing at work.

Background

This code should be fairly interesting for anybody that wants a simple example of how to make a control in C#. There's probably more elaborate examples here on CP, but I'm interested in simple things. Because I'm simple...

Using the code

The code should be pretty easy to use. As with all controls, you can drag the dll into your toolbox in Visual C#, or whatever development environment you're using. Then, you can drag the controls onto your form. I'll go through the code for each control separately.

SplashScreen

The SplashScreen class is really simple to use. All you need to do is create an object by calling the constructor and passing it a filename of an image, and then calling the ShowSplashScreen method. You pass the amount of time you want to show the splash screen (in msec) to the method, and it shows it for that amount of time. Also, if the user clicks the splash screen before time expires, it'll dispose itself and return control to your application.
// I'd put this code in your form's constructor, right after InitializeComponent()
SplashScreen splash = new SplashScreen(@"..\..\wdilogo.png");
splash.ShowSplashScreen(3000);

You might be saying, "isn't the point of a splash screen to show an image while my application is loading?" That's true, but my applications load instantly because I'm such a good programmer. I still wanted to have the fun of a splash screen, so I wrote this class. You could create a thread to initialize your application and then show the splash screen.

PixelProgressBar

This one's pretty exciting. I've seen a lot of great articles on progress bars here, but I wanted something a little more general. So, I created this class to show progress, but to progressively reveal an image as the progress increases. Plus, I'm into the whole isometric pixel art thing, so I made it display one of those images. The image I'm using could use some work, but I'm no artist. Anyways, here's how to use it: In this code I've set up a timer called timer1. Here's the code that starts the progress:
pixelProgressBar1.Max = max;
timer1.Enabled = true;

So you really just need to set the maximum value the progress bar can go to, and then turn the timer on. Obviously, you also need to have something happen when the timer ticks:

if (!pixelProgressBar1.Step())
{
  timer1.Enabled = false;
  MessageBox.Show("Done!");
}

The Step method returns the status of the progress bar, so when it returns false, we know that we're done, and we can turn off the timer and display a message.

PieGraph

Here's a pretty good one. I wanted a pie chart control that was simple to use. So, I wrote this one. To use it, all you need to do is give it an array of percentages, and an array of string descriptions. Like this:
double[] v = { 25.0, 15.0, 30.0, 30.0 };
string[] t = { "twenty-five", "fifteen", "thirty", "forty" };
pieGraph1.SetValues(v, t);
How easy is that!?

Guess what! There's more! An added feature of this control is that it can return a Bitmap object of the current state of the graph. Then, you can save your graph to a file, like this:

string filename = @"..\..\pie.bmp";
Bitmap pie = pieGraph1.GetBitmap();
pie.Save(filename);
Wow!

Points of Interest

Interesting things in PieGraph

Writing this control was fun for me. I learned a few new things about GDI+, that I probably never would have. First, there's the Graphics.FillPie method. I was making my brain hurt trying to think of a way to draw a pie chart slice until I found this method. All you do is tell it an angle and a bounding box and it draws it for you!

// last_angle is the start angle, and vals[i] is the sweep angle
g.FillPie(brushes[i], 0, 0, this.Width, this.Height, last_angle, (float)vals[i]);

Drawing rotated text was also an interesting thing to figure out. The following code draws text at a specified angle:

Font f = new Font("Helvetica", 14.0f);
Point center = new Point(this.Width / 2, this.Height / 2);
g.TranslateTransform(center.X, center.Y);
last_angle = 0.0f;
float x = 0.0f;
for (int i = 0; i < vals.Length; i++)
{
   string s = t[i];
   g.RotateTransform((float)vals[i]);
   StringFormat sf = new StringFormat(StringFormatFlags.NoClip);
   sf.Alignment = StringAlignment.Near;
   sf.LineAlignment = StringAlignment.Far;
   SizeF str_size = g.MeasureString(s, f, 150, sf);
   x = (this.Width / 2) - str_size.Width;
   g.DrawString(s, f, Brushes.White, x, 0, sf);
}
You might also notice the string measuring code in there. That's because I wanted to draw the descriptions to be right justified in the pie slice. Initially, I had all strings starting at the center of the circle, but this made the graph look too crowded. Moving them out to the edge made things look a lot better, as you can see in the screenshot at the head of this article.

History

UPDATE

With the help of some user comments, I found a bug in the SplashScreen drawing method.
e.Graphics.DrawImageUnscaled(image, 0, 0); Scales the image before drawing it!

No improvements! Everything's perfect!

Actually, there are a few issues I already know about. Like, the progress bar will never advance if you set the maximum value to be > 300, and the pie chart doesn't check if you've given it percentages that add up to more than 100. If anybody uses this code, I'll consider fixing things up.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Whoa! I'm thebeekeeper. I'm from Milwaukee, WI but I'm living in Boston, MA right now. I work for a very large organization doing digital signal processing, but sometimes I write programs for computers!

Go look at my web-site! (thebeekeeper.net)

Comments and Discussions

 
GeneralThanks Pin
Gmust19-Jul-10 0:39
Gmust19-Jul-10 0:39 
GeneralPieGraph GetBitmap() question Pin
Andy Wong NZ26-Feb-09 17:15
Andy Wong NZ26-Feb-09 17:15 
GeneralRe: PieGraph GetBitmap() question Pin
Andy Wong NZ26-Feb-09 17:48
Andy Wong NZ26-Feb-09 17:48 
GeneralRe: PieGraph GetBitmap() question Pin
thebeekeeper27-Feb-09 4:01
thebeekeeper27-Feb-09 4:01 
GeneralSplash Picture problem Pin
ShiroAmada1238-Feb-07 21:35
ShiroAmada1238-Feb-07 21:35 
GeneralRe: Splash Picture problem Pin
thebeekeeper9-Feb-07 3:37
thebeekeeper9-Feb-07 3:37 
GeneralRe: Splash Picture problem Pin
ShiroAmada12312-Feb-07 15:03
ShiroAmada12312-Feb-07 15:03 
GeneralRe: Splash Picture problem Pin
thebeekeeper13-Feb-07 3:56
thebeekeeper13-Feb-07 3:56 

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

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