我正在尝试创建一个从0%开始的进度条,需要5秒才能达到100%.单击Button1后,进度条将开始上升.有什么建议?我看了谷歌,但这对我这件事没什么好处.
此外,在0%时,应该有一个标签Waiting...
,当进度条开始时,它应该转到Working...
,当它完成时,它应该说Done!
.
您可以使用间隔为50的计时器,并首先将enabled设置为false.
procedure TForm1.Button1Click(Sender: TObject); begin timer1.Enabled := True; end; procedure TForm1.Timer1Timer(Sender: TObject); const cnt: integer = 1; begin ProgressBar1.Position := cnt; if cnt = 1 then Label1.Caption := 'Waiting...' else if cnt = 100 then begin Label1.Caption := 'Done!'; Timer1.Enabled := False; end else Label1.Caption := 'Working...'; Inc(cnt); end;
使用GetTickCount()和初始化变量:
uses Windows; var mseconds, starttime: integer; procedore TForm1.FormCreate() begin starttime := GetTickCount(); mseconds := 0; Timer1.Enabled := false; Label1.Caption := ''; ProgressBar1.Position := 0; Label1.Caption := 'Waiting...'; end; procedure TForm1.Button1Click(Sender: TObject); begin ProgressBar1.Min := 0; ProgressBar.Max := 100; ProgressBar1.Position := 0; timer1.Enabled := True; Label1.Caption := 'Working...'; end; procedure TForm1.Timer1Timer(Sender: TObject); begin mseconds := GetTickCount() - starttime; if mseconds < 5000 then ProgressBar1.Position := Trunc(mseconds / 50) else begin ProgressBar1.Position := 100; Label1.Caption := 'Done!'; Timer1.Enabled := false; end; end;