This component will help you to create a little chart in the System Tray. This is useful in applications like GetRight, which show progress of an operation (eg downloading). It can also show some system information (like CPU usage). NotifyIconChart provides three types of charts: single bar, two bars, and pie.
First, add NotifyIconChart.cs to your project. The NotifyIconChart is placed in System.Windows.Forms namespace, so it won’t make any trouble with namespaces in your solution.
You have to add to your form class a System.Windows.Forms.NotifyIcon object. I would like to inherit NotifyIconChart from the NotifyIcon, but it’s sealed (you can’t inherit from the sealed class). So I decided that NotifyIconChart would have a member pointer to a NotifyIcon object.
In the form constructor or Load event, set the NotifyIconChart’s NotifyIconObject property:
private NotifyIconChart notifyChart = new NotifyIconChart();
NotifyIcon notifyIcon = new NotifyIcon();
notifyChart.NotifyIconObject = notifyIcon;
Now set values. There are two properties that you can use: Value1 and Value2. The first bar, single bar or the pie will be drawn using the first value. The Value2 is used only when drawing the second bar. The values take integers between 0 and 100.
notifyChart.Value1 = 75;
notifyChart.Value2 = 43;
How does it work?
The NotifyIconChart uses a NotifyIcon object, which is responsible for displaying the chart. It gets only System.Drawing.Icon. That mean that NotifyIconChart has to create the icon. The .Net framework uses GDI+ for drawing. The Graphics object can’t draw on an Icon, but it can draw on a Bitmap. The NotifyIconChart draws on a bitmap (size 16x16) bars or a pie. It uses Graphics’ functions FillRectangle and FillPie. At the end it convert the Bitmap to an Icon and pass it to the NotifyIcon object:
Icon icon = Icon.FromHandle(bitmap.GetHicon());
notifyIcon.Icon = icon;
Changing type of the chart
As I said, there are three types of charts provided by NotifyIconChart: single bar, two bars, and pie. To change the type, just change the ChartType value:
notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.singleBar; notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.twoBars; notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.pie;
There are four properties that you can use to change colors:
Color2 are responsible for the bars’ colors. The pie will be drawn using Color1.
FrameColor is responsible for frames around bars and pie.
BackgroundColor is the color of the background.
NotifyIconChart draws rectangular background when it draws bars, and circular when it draws pie.
If you don’t want the
NotifyIconChart to draw any of these element’s set it’s value to
notifyChart.FrameColor = Color.Black;
notifyChart.Color1 = Color.Yellow;
System Tray isn’t the best place for charts, because it is rather small. But sometimes chart can be very useful there. Thanks to GDI+ functions like anti-aliasing, even a pie chart looks great. If you want any other type of chart in your System Tray, just give me a sign.