views:

25

answers:

1

I'm trying to make a program in C# that monitors the processor time of an application. I'd like to have a feature in this program where the task-bar icon is actually a number representing the processor time (like what coretemp has). How would I go about doing something like this?

+2  A: 

You need to use the classes in the System.Drawing namespace.

Create a Bitmap, use Graphics.FromImage to draw on it, then call Icon.FromHandle(myBitmap.GetHicon()).
You can then set the icon as your form's Icon property.

EDIT: Like this:

using (var image = new Bitmap(16, 16)) 
using (var g = Graphics.FromImage(image)) {
    g.DrawString(...);
    myForm.Icon = Icon.FromHandle(image.GetHicon());
}

You can run this in a timer to continually update the icon.

You can get the CPU usage like this:

int usage;
using (var cpu = new PerformanceCounter("Processor", "% Processor Time", "_Total")) {
    cpu.NextValue();            //First value is 0
    usage = cpu.NextValue();
}
SLaks
Okay, so if I'm reading this right, I would do something like this:Bitmap b = new Bitmap(Graphics.FromImage(someImage));Icon.fromHandle(b.getHIcon());I guess my only concern now is how to get the image itself - is there any easy way to tell C# to transform the processor time value into an image to use? The way I'm seeing this, and correct me if I'm wrong, is that Graphics.FromImage() insinuates I have to make the number icons myself.Thanks for all the help so far, though. (And sorry if the code above is hard to read, I'm not sure if I can insert code blocks into comments.)
Waffles
Wow, that worked! Thanks so much, I really learned something about this today. :)
Waffles