Click here to Skip to main content
Licence 
First Posted 31 Aug 2002
Views 114,336
Bookmarked 127 times

NotifyIconChart

By | 31 Aug 2002 | Article
A .NET component that creates pie and bar charts in the system tray

Sample Image

Introduction

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.

Using NotifyIconChart

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:

// Convert Bitmap to an Icon
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; // Draw one only bar
notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.twoBars; // Draw two bars
notifyChart.ChartType = NotifyIconChart.ChartTypeEnum.pie; // Draw pie chart

Colors

There are four properties that you can use to change colors:

Color1 and 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 Color.Transparent.

notifyChart.FrameColor = Color.Black;
notifyChart.Color1 = Color.Yellow;

Conclusion

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.

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

About the Author

Maciej Pirog

Web Developer

Poland Poland

Member

I've been programming since I was really young. Started with HTML and Basic, looked at Pascal and Java, keen on Visual Basic and Visual C++, thinking about .NET and C#.

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
General16 bars one for each vertical row in the icon PinmemberMember #27709776:01 10 Jan '07  
Hello
 
I added a new Draw routing to your NotifyIconChart.cs. This draw routing displays 16 bars one for each of the rows in a 16x16 bit icon. The draw routine displays the new data (entries just added) on the right side of the icon making it display like a chart. Please be advised that I may have changed some of your variable names.
 
            // A pointer to the current first bar in the icon 1-16
            private int m_iPointer = 0;
            private int[] m_iValues = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 
            public int AddValue
            {
                  get
                  {
                        return m_iValues[m_iPointer];
                  }
                  set
                  {
                        // Append the value to the array
                        m_iValues[m_iPointer] = value;
                        // Draw the new chart
                        CreateIcon();
                        // Update the array pointer, and check that it has not moved past the end
                        // of the array bounds
                        m_iPointer++;
                        if (m_iPointer >= size )
                        {
                              m_iPointer = 0;
                        }
                  }
            }
 

 
#if DEBUG
            private string sDebugOut = "";
 
            public void DebugPrintArray()
            {
                  sDebugOut = "";
                  Debug.WriteLine("---------------------");
                  Debug.WriteLine("Internal Array Values");
 
                  // Print the header first
                  int iCount = 0;
                  foreach (int iVal in m_iValues)
                  {
                        sDebugOut += iCount.ToString();
                        iCount++;
                        sDebugOut += "\t";
                  }
                  Debug.WriteLine(sDebugOut);
                  sDebugOut = "";
 
                  // Print the Values next
                  foreach (int iVal in m_iValues)
                  {
                        sDebugOut += iVal.ToString();
                        sDebugOut += "\t";
                  }
                  Debug.WriteLine(sDebugOut);
                  sDebugOut = "";
            }
#endif
 
            // Draw one bar for each value
            public void DrawSixteenBars()
            {
                  int iPosition = 0;
                  int[] iCalculatedValue = new int[m_iValues.Length];
                  int iArrayValue;
 
                  // Coordinates into the icon 16x16
                  int iWidth;
                  int iHeight;
                  int iY;
                  int iX;
                 
 
#if DEBUG
                  DebugPrintArray();
#endif
                  // Draw background
                  DrawBG(BGType.rect);
                 
                  // Set GDI+ objects
                  Pen pPen = new Pen(color1, 1);
                  SolidBrush bBrush = new SolidBrush(color1);
 
                  // Iterate through the int Array calculating the values corrected to fit in a 16x16 box (icon)
                  // the height maximum is 16 bits 1-100 fit into 0-16 bits heigh
                  for (int iCount = 0; iCount < m_iValues.Length; iCount++)
                  {
                        // The maximum value is a value of 16 bits
                        iArrayValue = m_iValues[iCount];
                        iCalculatedValue[iCount] = (size * iArrayValue) / 100;
                  }
 
#if DEBUG
                  DebugPrintArray();
                  Debug.WriteLine("iPointer Position " + m_iPointer.ToString());
                  sDebugOut = "iPosition" + " " + "iX" + " " + "iY" + " " + "iWidth" + " " + "iHeight";
                  Debug.WriteLine(sDebugOut);
#endif
 
                  // This is the desplay loop for the Icon 16x16 box, the last entry added to the list of
                  // values is stored in iPosition this value will be displayed in the 16th row or last line to
                  // the far right of the icon (iPosition == 16th row in the icon)
                  // Example   m_iPosition = 4               16
                  //                                 = 3               15
                  //                                 = 2               14
                  //                                 = 1               13
                  //                                 = 0               12
                  //                                 = 15            11
                  //                                 = 14            10
                  //                                 = 13            9
                  //                                 = 12            8
                  //                                 = 11            7
                  //                                 = 10            6
                  //                                 = 9               5
                  //                                 = 8               4
                  //                                 = 7               3
                  //                                 = 6               2
                  //                                 = 5               1
 
                  // Save a working copy of the graph start position
                  iPosition = m_iPointer;
                  for (int iCount = 16; iCount >= 0; iCount--)
                  {
                        // What direction is X?
                        iX = iCount;
                        //??
                        iY = size - iCalculatedValue[iPosition]; // 0; // size - width;
                        // The width of the Rectangle is 1 or a line
                        iWidth = 1; // 16;
                        //??
                        iHeight = iCalculatedValue[iPosition]; // size - width;
#if DEBUG
                        sDebugOut = iPosition.ToString() + " -->\t" + iX.ToString() + "\t" + iY.ToString() + "\t" + iWidth.ToString() + "\t" + iHeight.ToString();
                        Debug.WriteLine(sDebugOut);
#endif
 
                        // Brush, X, Y, Width, Height
                        g.FillRectangle(bBrush, iX, iY, iWidth, iHeight);
                        g.DrawRectangle(pPen, iX, iY, iWidth, iHeight);
 
                        // decrement the position counter
                        iPosition--;
                        if (iPosition < 0)
                        {
                              // reposition at the top of the array counting down, we have looped
                              iPosition = iCalculatedValue.Length - 1;
                        }
                  }
                  // Create and display icon
                  ShowIcon();
 
            }
 

            private void CreateIcon()
            {
                  // Draw different charts
                  if (chartType == ChartTypeEnum.singleBar)
                  {
                        DrawBars(value1);
                  }
                  else if (chartType == ChartTypeEnum.twoBars)
                  {
                        DrawBars(value1, value2);
                  }
                  else if (chartType == ChartTypeEnum.pie)
                  {
                        DrawPie(value1);
                  }
                  else if (chartType == ChartTypeEnum.sixteenBars)
                  {
                        DrawSixteenBars();
                  }
 
            }
 
At the same time I created a test application that allowed me to test your code more effectively if you would like the code drop me a note and I will send it to you.
 
Robert
 

 

GeneralnotifyIcon.Visible = true; PinmemberToothRobber4:14 9 Jan '07  
GeneralTidying up PinmemberDrew Noakes6:18 7 Apr '05  
GeneralRe: Tidying up PinmemberdzCepheus11:48 4 Sep '05  
GeneralNice Pinmemberngna20:35 19 Sep '04  
GeneralRe: Nice PinmemberMaciej Pirog7:51 1 Oct '04  
GeneralCopy cursor into Bitmap - C# Pinmemberjayakarthikeyan1:45 17 Jun '04  
GeneralCopy cursor into Bitmap Pinmemberjayakarthikeyan1:40 17 Jun '04  
GeneralThis is excellent! PinmemberCavaradossi0:44 21 Aug '03  
GeneralRe: This is excellent! PinmemberMaciej Pirog2:36 21 Aug '03  
GeneralAlles klar Pinsusslukasz stypka2:16 4 Mar '03  
GeneralUse composition to create NotifyIcon. PinmemberPeter Rilling4:06 25 Jan '03  
GeneralRe: Use composition to create NotifyIcon. PinmemberMaciej Pirog23:37 1 Feb '03  
GeneralScrolling Chart like DuMeter PinsussAnonymous10:13 9 Nov '02  
GeneralGetHicon - A general error occurred in GDI+ PinmemberIngram Leedy21:03 18 Oct '02  
GeneralRe: GetHicon - A general error occurred in GDI+ PinmemberMaciej Pirog5:42 19 Oct '02  
GeneralRe: GetHicon - A general error occurred in GDI+ PinmemberNewtonTroy9:08 3 Nov '03  
GeneralRe: GetHicon - A general error occurred in GDI+ Pinmembermrblueguy0:14 11 Nov '03  
GeneralRe: GetHicon - A general error occurred in GDI+ Pinmemberparagvt13:01 27 Jun '05  
GeneralRe: GetHicon - A general error occurred in GDI+ Pinmemberorumcektr4:26 2 Aug '06  
GeneralNice! PinmemberTim Hodgson3:55 3 Sep '02  
GeneralRe: Nice! PinmemberMaciej Pirog7:26 3 Sep '02  
Generalcze&#347;&#263; PinsussARTUR PIRÓG2:12 18 Oct '03  

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
Web02 | 2.5.120529.1 | Last Updated 1 Sep 2002
Article Copyright 2002 by Maciej Pirog
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid