Click here to Skip to main content
Click here to Skip to main content

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

By , 8 May 2008
 

Introduction

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.

Background

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
[StructLayout(LayoutKind.Sequential)]
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;
}
...
MEMORY_INFO MemInfo;
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 = 
        System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
    //Background
    e.Graphics.DrawImage
        (Image, (int)positionRect.X, (int)positionRect.Y, 198, 159);
    //Point
    e.Graphics.ResetTransform();
    e.Graphics.TranslateTransform
        (positionRect.X + 68f, positionRect.Y + 82f);
    e.Graphics.RotateTransform(cpuCurAngle);
    e.Graphics.DrawImage(ImageDial, -5, -49, 10, 98);
    e.Graphics.ResetTransform();
    e.Graphics.TranslateTransform
        (positionRect.X + 143f, positionRect.Y + 50f);
    e.Graphics.RotateTransform(memCurAngle);
    e.Graphics.DrawImage(ImageDialSmall, -5, -35, 10, 70);
    e.Graphics.ResetTransform();
    //Cover
    e.Graphics.DrawImage
        (ImageDialDot, (int)positionRect.X, (int)positionRect.Y, 198, 150);
    //Text
    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);
    //GlassEffect
    e.Graphics.DrawImage
        (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.

History

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

License

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

About the Author

Davidwu
China China
Member
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalthanksmemberjishadshajahan20hrs 5mins ago 
Generalvery nice work !memberBillWoodruff4 Apr '08 - 16:42 
GeneralRe: very nice work ! [modified]memberDavidwu8 May '08 - 21:18 
QuestionGreat! may I use it?memberWin32nipuh11 Feb '08 - 19:55 
AnswerRe: Great! may I use it?memberDavidwu13 Feb '08 - 20:01 
Of course you can! my pleasure!Smile | :)
GeneralRe: Great! may I use it?memberWin32nipuh13 Feb '08 - 20:24 
GeneralRe: Great! may I use it?memberDavidwu14 Feb '08 - 15:29 
GeneralI have thought of doing this meter control beforememberWong Shao Voon4 Feb '08 - 17:22 
GeneralRe: I have thought of doing this meter control beforememberDavidwu10 Feb '08 - 1:52 
GeneralA Tip for you... (applies to both your clock control and the CPU Info control here)memberJohnny J.3 Feb '08 - 23:00 
GeneralRe: A Tip for you... (applies to both your clock control and the CPU Info control here)memberDavidwu10 Feb '08 - 1:48 
GeneralWowmember Dr.Luiji 3 Feb '08 - 20:58 
GeneralRe: WowmemberDavidwu10 Feb '08 - 1:44 
GeneralContext menu items checked properties not updatedmemberDaveyM6923 Jan '08 - 23:13 
GeneralRe: Context menu items checked properties not updatedmemberDavidwu27 Jan '08 - 10:21 
QuestionIs the URL necessary?memberMark Nischalke23 Jan '08 - 5:29 
AnswerRe: Is the URL necessary?memberDavidwu23 Jan '08 - 6:49 
AnswerRe: Is the URL necessary?memberHerbert Sauro18 Aug '08 - 18:16 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 9 May 2008
Article Copyright 2008 by Davidwu
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid