德尔福:平滑崩溃/扩展形式

时间:2016-04-30 10:20:29

标签: function delphi

需要你的帮助(我一直在寻找)。我在德尔福西雅图,试图平滑调整表格的底部。在我的情况下,“调整大小”只是有点像这样崩溃/扩展:

enter image description here

我怎么能意识到这一点?

我尝试过使用TTimer:

procedure TForm1.Timer1Timer(Sender: TObject);
var
h, t: integer;
begin
t := Button10.Top + Button10.Height + 10; //slide TForm from/to this point
if t > h then
begin
h := h + 1;
Form1.Height := h;
end
else
begin
Timer1.Enabled := false;
end;
end;

...但它看起来非常简单(没有加速/减速),即使间隔很小也会很慢。

1 个答案:

答案 0 :(得分:6)

没有必要让TTimers变得复杂。这将包括折叠和扩展形式,包括您需要的平滑度。

诀窍是通过在每次迭代中获取目标大小 - 当前高度和div 3来计算每个步骤,这将加速初始崩溃或扩展,然后随着形式接近其目标大小而减速。

procedure TForm1.SmoothResizeFormTo(const ToSize: integer);
var
  CurrentHeight: integer;
  Step: integer;
begin
  while Height <> ToSize do
  begin
    CurrentHeight := Form1.Height;

    // this is the trick which both accelerates initially then 
    // decelerates as the form reaches its target size
    Step := (ToSize - CurrentHeight) div 3; 

    // this allows for both collapse and expand by using Absolute
    // calculated value
    if (Step = 0) and (Abs(ToSize - CurrentHeight) > 0) then
    begin
      Step := ToSize - CurrentHeight;
      Sleep(50); // adjust for smoothness
    end;

    if Step <> 0 then
    begin
      Height := Height + Step;
      sleep(50); // adjust for smoothness
    end;
  end;
end;

procedure TForm1.btnCollapseClick(Sender: TObject);
begin
  SmoothResizeFormTo(100);
end;

procedure TForm1.btnExpandClick(Sender: TObject);
begin
  SmoothResizeFormTo(800);   
end;