Author: Alex Schlecht
The TStatusbar usually does not allow to place components on itself. But sometimes
it would be very fine to add -for example- a TProgressBar on a Statusbar.
This Article shows how to add components to a TStatusbar and how to fit it into one
of the Statusbar-Panels.
Answer:
There are (at least) two ways to add Components on your Statusbar:
1. Create an own Statusbar-Object
Create your own Statusbar and allow to add components on it. This is possible in
overriding the Create-Constructor:
1 type2 TMyStatusBar = class(TStatusBar)
3 public4 constructor Create(AOwner: TComponent); override;
5 end;
6 7 implementation8 9 constructor TMyStatusBar.Create(AOwner: TComponent);
10 begin11 inherited Create(AOwner);
12 ControlStyle := ControlStyle + [csAcceptsControls];
13 //that’s all !!14 end;
That’s all! Now this component accept other components as “Children” and you can
put them at design-time onto the statusbar!
But I don’t like this way very much because you have to use this new component. I
prefer to use the “old” components and manipulating them a little bit. So lets have
a look to my favourite way:
2. “Adopt” the other component
The simplest way to include components to a statusbar is to adopt the component!
Place the TStatusbar on your Form, also place the Progressbar (or other component
you wish to include on your Statusbar) on the form (!). Then do this in the
“OnShow” Event of the Form:
15 Progressbar1.Parent := statusbar1;
16 Progressbar1.top := 1;
17 Progressbar1.left := 1;
Now the Progressbar is “adopted” by the Statusbar.
But unfortunatley it doesn’t look very nice because the Progressbar is larger than
the panel and the position is not correct. So we have to determine the exact
position of the Progresbar by using the Statubar’s border, width and height. (We
have to add this code to the “OnShow” Event of the form, because in the “OnCreate”
event still no Handles are avalible.)
18 procedure TForm1.FormShow(Sender: TObject);
19 var20 r: TRect;
21 begin22 Statusbar1.perform(SB_GETRECT, 0, integer(@R));
23 //determine the size of panel 124 25 //SB_GETRECT needs Unit commctrl26 // 0 = first Panel of Statusbar; 1 = the second and so on.27 28 progressbar1.parent := Statusbar1; //adopt the Progressbar29 30 progressbar1.top := r.top; //set size of31 progressbar1.left := r.left; //Progressbar to32 progressbar1.width := r.right - r.left; //fit with panel33 progressbar1.height := r.bottom - r.top;
34 35 end;
Now the Progressbar fits exactly into the first panel of the statusbar! If you want to use the second or another panel, you only have to change the parameter of the “perform” command.