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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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?
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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?
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WaitingWonder2Author Commented:
Cool. Thanks a lot. Looks like original Task Manager bar now. :)
WaitingWonder2Author Commented:
Thanks again for being patient.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.