a component in delphi that shows CPU usage


I want to write a component in delphi that will show CPU usage, see picture. I just wondering what is the best class to make a descendant from since I want to make it with a caption CPU usage and outer border and inner border that will divide black graph area from panel area like Windows Task Manager has.
Who is Participating?
Emmanuel PASQUIERFreelance Project ManagerCommented:
I'm not too fond of that 1 over 2 pixels drawing. That looks like interface when computers had only 16 colors.
I would draw bars in darker green, but solid :
Brush.Color:=$004000; for example (25% green)

otherwise, no choice than put pixels
procedure FillRectWith1Over2Pixel(Canvas:TCanvas;R:TRect;Col1,Col2:TColor);
 Col:Array[0..1] Of TColor;
// -1 on Bottom & Right because of that weird choice of MS to exclude
// the last right and bottom lines for all Rectangle, FillRect etc.. 
// drawing functions. So, for compatibility...
 For Y:=R.Top to R.Bottom-1 do 
  For X:=R.Left to R.Right-1 do 
   Canvas.Pixels[X,Y]:=Col[(X+Y) And 1];

Open in new window

Emmanuel PASQUIERFreelance Project ManagerCommented:
your component should not include the caption and border. That is a TGroupBox, and I see no good reason to re-do it. And what if you suddenly don't want to show the border ? or a different way ?
No, your component should only be the graph area, without the black area around and the label below.
Those are easily placed and moved after the graph component is done.
If you want to do once and for all such a complete and fixed interface as you wish, you can create a Frame with all the components I said : the graph and a label for CPU%, both within a black panel, itselt inside a GroupBox. And probably an event to sync the graph and the label. That's it.

That said, I think that a CPU meter component exist already, probably more than once. Why do you want to do one ? for fun and just to know how (that would be a good enough reason for me) ?
Emmanuel PASQUIERFreelance Project ManagerCommented:
Ah, I didn't say : you graph component should therefore derive from TGraphicControl, because it won't need focus and key event. That way it will be easier to create than with TWinControl, and will use less resources.

You can start by looking the source code of TShape in Delphi units, if you have them. That's a simple TGraphicControl, and it has everything you need to know to do your own control.
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

WaitingWonder2Author Commented:
I did not find such kind of a component, so decided to write myself. I found the simular but not the exact like I wanted and it wasn't running under D2007 since author gave it up and there were no sources given. I did find several components that shows CPU history, so I need only left graph.

I inherited from TPaintBox for the black area instead of TGraphicControl. Or you think TGraphicControl will be better?

Do you think it is better to inherite from TPanel instead of TGroupBox to create a panel I wanted? Why?
Emmanuel PASQUIERFreelance Project ManagerCommented:
Here are a few useful source :

Get CPU usage : http://www.aldyn.ru/products/cpu_usage/index.html
'Leds bar' (or gauge) component : http://www.torry.net/vcl/indicat/meters/raled.zip

more can be found in http://www.torry.net/pages.php?id=504
but these are the ones I selected for you to get started
Emmanuel PASQUIERFreelance Project ManagerCommented:
TPaintBox is an easy way to test a drawing. It inherits from TGraphicControl, and the only difference is that the painting is done from 'outside' of the control, within the onPaint event.
A TGraphicControl descendent, on the other hand, will do the painting by overriding the Paint method (which is the one calling the onPaint in TPaintBox). So a TGraphicControl HAVE TO define its aspect for any given state, and not rely on other piece of application to decide for them. That is what you want.
See the RALed component above, it is a perfect example to what you want. Combine it with CPUUsage, change the drawing to your liking and you have your own CPUMeter
WaitingWonder2Author Commented:
The RALed component is almost what I want to do except that I'll have a label, 2 bars and a bar is dotted in other way in Task Manager when showing in inactive color. It looks like it consists of blocks that have 2 green points, 2 black points on the first line and 2 black points and 2 green point on the second line. Do you know how that could be done?

And please, answer this question: Do you think it is better to inherite from TPanel instead of TGroupBox to create a panel I wanted? Why?
Emmanuel PASQUIERFreelance Project ManagerCommented:
you don't inherit once you have created your graphic meter. You just assemble components in a Frame. If you don't know about frame, then you should try to create one (File -> New ... Frame ) and see how it works. Basically it is a 'piece of form' where you can put whatever components in it, define events like in a form etc... and when you are done, you can add it to the component palette (right click on the Frame) and reuse it at will from any application.
That's very worth the time to try it.
Emmanuel PASQUIERFreelance Project ManagerCommented:
you can start with the RALed, a timer, and call in its event CPUUsage functions to set the RALed position to the value of the first CPU usage, and see how it goes.
Then you create your own meter component to fit the look you want, add a GroupBox, a label etc..
You can also dynamically create the correct number of meter and labels to fit the number of CPU, and dynamically change the size of the frame
WaitingWonder2Author Commented:
Yes, the whole solution was clear from the beginning, I just need the guidance on the details, what class is better for inheritance and so on. Seems to me, you explained everything. I just need to figure out how to set dotted inactive bar like in Task Manager (not in RALed), I mean dark green part of the bar, it is a bit different. I have the idea that I should fill the top half of the rectangle in green for odd rectangle and the bottom half in green for even rectangle. Just wondering is there a better idea?
WaitingWonder2Author Commented:
Cool. Thanks a lot. Looks like original Task Manager bar now. :)
WaitingWonder2Author Commented:
Thanks again for being patient.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.