游子日月长

笑渐不闻声渐悄,多情却被无情恼!

导航

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;

posted on 2017-02-14 16:57  游子日月长  阅读(322)  评论(0编辑  收藏  举报