Click here to Skip to main content
15,885,757 members
Articles / Mobile Apps / Windows Mobile

A Cool Vista Sidebar Gadget Style CPUInfo Animate Control! (Fixed)

Rate me:
Please Sign up or sign in to vote.
4.68/5 (72 votes)
8 May 2008CPOL 100K   2.5K   140   19
A Cool Vista Sidebar Gadget Style CPUInfo Animate Control! (Fixed)
Image 1


This article introduces how to make a Vista style cool CPUInfo control with C#. This control can get a percentage of CPU and Memory of the current Windows system.


Microsoft provides a beautiful sidebar in the Vista System, so I want to make the same control with C#. Thanks to GDI+, you can find that it is very easy to do so.

Using the Code

The main class is the VistaCPUInfo class; it is inherited from the Usercontrol class:

// Main Class
public partial class VistaCPUInfo : UserControl

To get the percentage of CPU, use the following code:

if (pc == null) pc = new PerformanceCounter("Processor", "% Processor Time", "_Total");
cpu = (float)pc.NextValue();  //This is the aim value!

To get the percentage of physical memory, use the following code:

//Memory Structure
public struct MEMORY_INFO
    public uint dwLength;
    public uint dwMemoryLoad;
    public uint dwTotalPhys;
    public uint dwAvailPhys;
    public uint dwTotalPageFile;
    public uint dwAvailPageFile;
    public uint dwTotalVirtual;
    public uint dwAvailVirtual;
MemInfo = new MEMORY_INFO();
GlobalMemoryStatus(ref  MemInfo);
mem = (float)MemInfo.dwMemoryLoad;    //This is the aim value!

Then, use GDI+ methods to draw the result:

void VistaCPUInfo_Paint(object sender, PaintEventArgs e)
    e.Graphics.TextRenderingHint = 
        (Image, (int)positionRect.X, (int)positionRect.Y, 198, 159);
        (positionRect.X + 68f, positionRect.Y + 82f);
    e.Graphics.DrawImage(ImageDial, -5, -49, 10, 98);
        (positionRect.X + 143f, positionRect.Y + 50f);
    e.Graphics.DrawImage(ImageDialSmall, -5, -35, 10, 70);
        (ImageDialDot, (int)positionRect.X, (int)positionRect.Y, 198, 150);
    RectangleF rect = new RectangleF((int)positionRect.X + 53, 
                        (int)positionRect.Y + 107, 35, 15);
    e.Graphics.DrawString(((int)percentOfCPU).ToString() + 
                    "%", textFont, textBrush, rect, format);
    rect = new RectangleF((int)positionRect.X + 127, 
                (int)positionRect.Y + 66, 35, 13);
    e.Graphics.DrawString(((int)percentOfMemory).ToString() + 
                    "%", textFont, textBrush, rect, format);
        (ImageGlass, (int)positionRect.X, (int)positionRect.Y, 198, 159);

Points of Interest

  • Developing with GDI+ and C# is an very interesting thing!
  • PNG format picture is very good for drawing alpha pics!
  • For more code samples, please visit this Web site.


  • 2008/1/8: First posted on
  • 2008/5/9: Modified by Davidwu: disabled the animation in designtime mode. (thanks to Johnny J.!)


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

Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionLooks great but ... Pin
N. Henrik Lauridsen7-Oct-15 0:55
N. Henrik Lauridsen7-Oct-15 0:55 
When I try to run your control I get an error. See below.

I have tried the compiled exe version too. Same result

I am using Windows 10

Hope you have time to help me. Thank you in advance.

An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll

Additional information: Tælleren % Processor Time findes ikke i den angivne kategori.
Translated from Danish : The counter % Processor Time does not exist in the category
Debugger:Exception Intercepted: timer_Tick, VistaCPUInfo.cs line 213
An exception was intercepted and the call stack unwound to the point before the call from user code where the exception occurred.  "Unwind the call stack on unhandled exceptions" is selected in the debugger options.
Time: 07-10-2015 12:47:46
Thread:<No Name>[2664]

Generalthanks Pin
jishadshajahan21-May-13 1:51
jishadshajahan21-May-13 1:51 
Generalvery nice work ! Pin
BillWoodruff4-Apr-08 16:42
professionalBillWoodruff4-Apr-08 16:42 
GeneralRe: very nice work ! [modified] Pin
Davidwu8-May-08 21:18
Davidwu8-May-08 21:18 
QuestionGreat! may I use it? Pin
Win32nipuh11-Feb-08 19:55
professionalWin32nipuh11-Feb-08 19:55 
AnswerRe: Great! may I use it? Pin
Davidwu13-Feb-08 20:01
Davidwu13-Feb-08 20:01 
GeneralRe: Great! may I use it? Pin
Win32nipuh13-Feb-08 20:24
professionalWin32nipuh13-Feb-08 20:24 
GeneralRe: Great! may I use it? Pin
Davidwu14-Feb-08 15:29
Davidwu14-Feb-08 15:29 
GeneralI have thought of doing this meter control before Pin
Shao Voon Wong4-Feb-08 17:22
mvaShao Voon Wong4-Feb-08 17:22 
GeneralRe: I have thought of doing this meter control before Pin
Davidwu10-Feb-08 1:52
Davidwu10-Feb-08 1:52 
GeneralA Tip for you... (applies to both your clock control and the CPU Info control here) Pin
Johnny J.3-Feb-08 23:00
professionalJohnny J.3-Feb-08 23:00 
GeneralRe: A Tip for you... (applies to both your clock control and the CPU Info control here) Pin
Davidwu10-Feb-08 1:48
Davidwu10-Feb-08 1:48 
GeneralWow Pin
Dr.Luiji3-Feb-08 20:58
professionalDr.Luiji3-Feb-08 20:58 
GeneralRe: Wow Pin
Davidwu10-Feb-08 1:44
Davidwu10-Feb-08 1:44 
GeneralContext menu items checked properties not updated Pin
DaveyM6923-Jan-08 23:13
professionalDaveyM6923-Jan-08 23:13 
GeneralRe: Context menu items checked properties not updated Pin
Davidwu27-Jan-08 10:21
Davidwu27-Jan-08 10:21 
QuestionIs the URL necessary? Pin
Not Active23-Jan-08 5:29
mentorNot Active23-Jan-08 5:29 
AnswerRe: Is the URL necessary? PinPopular
Davidwu23-Jan-08 6:49
Davidwu23-Jan-08 6:49 
AnswerRe: Is the URL necessary? Pin
Herbert Sauro18-Aug-08 18:16
Herbert Sauro18-Aug-08 18:16 

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.