Delphi组件:在运行时动画显示/隐藏控件

5
在Delphi中,我可以在运行时显示/隐藏控件,但是它看起来并不好,因为控件突然出现或消失,所以有没有人知道一个组件,可以使用一些动画效果来显示/隐藏(使用可见属性)?
谢谢

你可以尝试的另一件事是在标签上添加闪烁文本。 - David Heffernan
1个回答

8

试试使用AnimateWindow。虽然只适用于WinControls,但它看起来也不是那么惊艳:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Button2.Visible then
    AnimateWindow(Button2.Handle, 250, AW_HIDE or AW_VER_NEGATIVE or AW_SLIDE)
  else
    AnimateWindow(Button2.Handle, 250, AW_VER_POSITIVE or AW_SLIDE);
  Button2.Visible := not Button2.Visible; // synch with VCL
end;


编辑:一个带有线程的版本可以同时隐藏显示多个控件:

type
  TForm1 = class(TForm)
    ..
  private
    procedure AnimateControls(Show: Boolean; Controls: array of TWinControl);
    procedure OnAnimateEnd(Sender: TObject);
  public
  end;

implementation
  ..

type
  TAnimateThr = class(TThread)
  protected
    procedure Execute; override;
  public
    FHWnd: HWND;
    FShow: Boolean;
    constructor Create(Handle: HWND; Show: Boolean);
  end;

{ TAnimateThr }

constructor TAnimateThr.Create(Handle: HWND; Show: Boolean);
begin
  FHWnd := Handle;
  FShow := Show;
  FreeOnTerminate := True;
  inherited Create(True);
end;

procedure TAnimateThr.Execute;
begin
  if FShow then 
    AnimateWindow(FHWnd, 250, AW_VER_POSITIVE or AW_SLIDE)
  else 
    AnimateWindow(FHWnd, 250, AW_HIDE or AW_VER_NEGATIVE or AW_SLIDE);
end;

{ Form1 }

procedure TForm1.OnAnimateEnd(Sender: TObject);
begin
  FindControl(TAnimateThr(Sender).FHWnd).Visible := TAnimateThr(Sender).FShow;
end;

procedure TForm1.AnimateControls(Show: Boolean; Controls: array of TWinControl);
var
  i: Integer;
begin
  for i := Low(Controls) to High(Controls) do
    with TAnimateThr.Create(Controls[i].Handle, Show) do begin
      OnTerminate := OnAnimateEnd;
      Resume;
    end;
end;


procedure TForm1.Button5Click(Sender: TObject);
begin
  AnimateControls(not Button1.Visible,
      [Button1, Button2, Button3, Edit1, CheckBox1]);
end;
 

这就够了,我不贪心 :) - 这是新的API吗?如果是,我该如何了解Delphi内支持的新Windows API -- 非常感谢 - user639478
1
@user - 其实 Winapi 自 Windows 98 起就存在了。它非常庞大,即使 Delphi 没有内置对某些功能的支持,你也可能会在别人那里找到相应的移植版本。我不知道如何跟踪 API 或 Delphi 的更改,当你需要某些东西时,你只能自己琢磨出来。 :) - Sertac Akyuz
1
@user - 我注意到'AnimateWindow'函数是同步的,也就是说它在动画完成之前不会返回。如果您需要同时对许多控件进行动画处理,这可能看起来不太好。在这种情况下,考虑取消接受答案以引起更多关注。顺便说一句,我尝试了一个线程版本...但它并不一致地工作... - Sertac Akyuz
1
非常感谢Sertac,我忘记了Delphi甚至有免费的线程组件 - 非常感谢 - 顺便问一下,这里的人们怎么找时间帮助其他人呢? - user639478

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接