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 开发者_Go百科has). How would I go about doing something like this?
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();
}
精彩评论