Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi component to animate show/hide controls during runtime

Tags:

delphi

In Delphi I show/hide controls during runtime and it does not look good as controls suddenly appear or disappear , so any one know a component that can do the show/hide (using visible property) but with some sort of animation ?

thanks

like image 305
user639478 Avatar asked Apr 17 '11 21:04

user639478


1 Answers

Give it a go with AnimateWindow. Only for WinControls, well, it doesn't look stunning anyway:

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;


edit: A threaded version to hide show multiple controls simultaneously:

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;
 
like image 88
Sertac Akyuz Avatar answered Sep 18 '22 21:09

Sertac Akyuz