Wednesday, 21 February 2018

Animate opening and closing a panel


Thought I would share an example of how I animate the opening and closing of a panel that can contain various controls and how each control can also be animated.



Here is the example code:

unit AnimatePanelFrm;

interface

uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls;

type
    TForm1 = class(TForm)
        Panel1 : TPanel;
        Shape1 : TShape;
        Label1 : TLabel;
        BitBtn1 : TBitBtn;
        BitBtn2 : TBitBtn;
        BitBtn3 : TBitBtn;
        BitBtn4 : TBitBtn;
        procedure Label1Click(Sender : TObject);
    private
        {Private declarations}
    public
        {Public declarations}
    end;

var
    Form1 : TForm1;

implementation

{$R *.dfm}

procedure TForm1.Label1Click(Sender : TObject);

const
    Expanded_Height = 106;
    Collapse_Height = 44;
    Speed           = 100;
    Speed2          = 200;
begin

    AnimateWindow(BitBtn1.Handle, Speed, AW_HOR_NEGATIVE OR AW_SLIDE OR AW_HIDE);
    AnimateWindow(BitBtn2.Handle, Speed, AW_HOR_NEGATIVE OR AW_SLIDE OR AW_HIDE);
    AnimateWindow(BitBtn3.Handle, Speed, AW_HOR_NEGATIVE OR AW_SLIDE OR AW_HIDE);
    AnimateWindow(BitBtn4.Handle, Speed, AW_HOR_NEGATIVE OR AW_SLIDE OR AW_HIDE);

    AnimateWindow(Panel1.Handle, Speed2, AW_VER_NEGATIVE OR AW_SLIDE OR AW_HIDE);
    if Panel1.Height = Expanded_Height then
        Panel1.Height := Collapse_Height
    else
        Panel1.Height := Expanded_Height;
    AnimateWindow(Panel1.Handle, Speed2, AW_VER_POSITIVE OR AW_SLIDE OR AW_ACTIVATE);

    AnimateWindow(BitBtn4.Handle, Speed, AW_VER_POSITIVE OR AW_SLIDE OR AW_ACTIVATE);
    AnimateWindow(BitBtn3.Handle, Speed, AW_HOR_POSITIVE OR AW_SLIDE OR AW_ACTIVATE);
    AnimateWindow(BitBtn2.Handle, Speed, AW_HOR_POSITIVE OR AW_SLIDE OR AW_ACTIVATE);
    AnimateWindow(BitBtn1.Handle, Speed, AW_HOR_POSITIVE OR AW_SLIDE OR AW_ACTIVATE);

end;

end.

No comments:

Post a Comment