Link to home
Start Free TrialLog in
Avatar of WaitingWonder2
WaitingWonder2

asked on

a component in delphi that shows CPU usage

Hello,

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.
CruUsage1.bmp
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

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) ?
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.
Avatar of WaitingWonder2
WaitingWonder2

ASKER

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?
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
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
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?
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.
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
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?
ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cool. Thanks a lot. Looks like original Task Manager bar now. :)
Thanks again for being patient.