Mega Code Archive

 
Categories / Delphi / VCL
 

Including Components into a StatusBar

Title: Including Components into a StatusBar Question: 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: First apologize my bad english: Im a programmer, not a translator ;-) 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: type TMyStatusBar = class(TStatusBar) public constructor Create(AOwner: TComponent); override; end; implementation constructor TMyStatusBar.Create(AOwner: TComponent); begin inherited Create(AOwner); ControlStyle:= ControlStyle + [csAcceptsControls]; //thats all !! end; Thats all! Now this component accept other components as Children and you can put them at design-time onto the statusbar! But I dont 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: Progressbar1.Parent := statusbar1; Progressbar1.top := 1; Progressbar1.left := 1; Now the Progressbar is adopted by the Statusbar. But unfortunatley it doesnt 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 Statubars 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.) procedure TForm1.FormShow(Sender: TObject); var r : TRect; begin Statusbar1.perform(SB_GETRECT,0,integer( @R )); //determine the size of panel 1 //SB_GETRECT needs Unit commctrl // 0 = first Panel of Statusbar; 1 = the second and so on. progressbar1.parent := Statusbar1; //adopt the Progressbar progressbar1.top := r.top; //set size of progressbar1.left := r.left; //Progressbar to progressbar1.width := r.right-r.left; //fit with panel progressbar1.height := r.bottom-r.top; 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. Comments and improvements are welcome!! Alex Schlecht Alex.schlecht@skw.com