2009. július 31., péntek

Putting a ProgressBar on a StatusBar


Problem/Question/Abstract:

Putting a ProgressBar on a StatusBar

Answer:

Many programs out there display a progress bar on the status bar. Internet Explorer is one of those. However, Delphi doesn't have component with that feature built-in. But that doesn't prevent us from having a progress bar inside a status bar panel. This is what this trick will tech you.

To make this tip work, create a form with a StatusBar (let's accept the default name: StatusBar1). Add a few panels to it.

To the public section of the form class declaration, add:

ProgressBar1: TProgressBar;

To the OnCreate event handler of the form, add:

var
  ProgressBarStyle: LongInt;
begin
  {create a run progress bar in the status bar}
  ProgressBar1 := TProgressBar.Create(StatusBar1);
  ProgressBar1.Parent := StatusBar1;
  {remove progress bar border}
  ProgressBarStyle := GetWindowLong(ProgressBar1.Handle, GWL_EXSTYLE);
  ProgressBarStyle := ProgressBarStyle - WS_EX_STATICEDGE;
  SetWindowLong(ProgressBar1.Handle, GWL_EXSTYLE, ProgressBarStyle);
  {set progress bar position and size - put in Panel[2]}
  ProgressBar1.Left := StatusBar1.Panels.Items[0].Width +
    StatusBar1.Panels.Items[1].Width + 4;
  ProgressBar1.Top := 4;
  ProgressBar1.Height := StatusBar1.Height - 6;
  ProgressBar1.Width := StatusBar1.Panels.Items[2].Width - 6;
  {set range and initial state}
  ProgressBar1.Min := 0;
  ProgressBar1.Max := 100;
  ProgressBar1.Step := 1;
  ProgressBar1.Position := 0;
end;
  
In the OnDestroy event handler of the form, add:

ProgressBar1.free;

If the position of the ProgressBar within the StatusBar doesn't please you, you can change the top, left, height and width properties of the ProgressBar. You can also change the Step, Max and Min properties of the ProgressBar. Within your program, work with the progress bar as you normally would, by accessing it's Position property.

Nincsenek megjegyzések:

Megjegyzés küldése