Delphi在StatusBar上绘制ProgressBar

时间:2023-03-08 21:37:30

Delphi在StatusBar上绘制ProgressBar

首先,在TForm的私有域,也就是private下设置两个变量ProgressBar、ProgressBarRect,其中ProgressBar为 TProgressBar类型,ProgressBarRect为TRect类型,完整的定义如下:

type
  TForm1 = class(TForm)
    ......
  private
    ProgressBar: TProgressBar;
    ProgressBarRect: TRect;
  end;

然后在StatusBar的DrawPanel事件中添加代码:

ProgressBarRect := Rect;

完整代码如下:

procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar;
  Panel: TStatusPanel; const Rect: TRect);
begin
  ProgressBarRect := Rect;
end;

最后我们用一个Button组件来测试,代码如下:

procedure TForm1.Button1Click(Sender: TObject);
var
  i, StepCount: integer;
begin
  ProgressBar := TProgressBar.Create(Form1);
  StepCount := 10000;
  with ProgressBar do
  begin
    Top := ProgressBarRect.Top;
    Left := ProgressBarRect.Left;
    Width := ProgressBarRect.Right - ProgressBarRect.Left;
    Height := ProgressBarRect.Bottom - ProgressBarRect.Top;
    Visible := True;
    //Smooth := True;

try
      Parent := StatusBar1;
      Min := 0;
      Max := StepCount;
      for i := 1 to stepCount do
      begin
        StepIt;
      end;
      MessageDlg('状态栏进度条演示操作已完成!', mtInformation, [mbOK], 0);
    finally
      Free;
    end;
  end;
end;

http://www.lsworks.net/article/43.html