Sorry, my previous code has calculation issue. If you set Steps = 30 or more then you will see what I mean.
Here is the better version:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, StdCtrls, ExtCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
private
procedure ZoomTo(TheOBject, Target: TControl; Steps: Integer);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
ZoomTo(Button1, Image1, 40);
end;
procedure TForm1.ZoomTo(TheOBject, Target: TControl; Steps: Integer);
const
oX: Integer = 0;
oY: Integer = 0;
oW: Integer = 0;
oH: Integer = 0;
var
dX, dY, dW, dH: Integer;
i: Integer;
begin
// Bad parameters
if not(Assigned(TheOBject)) then Exit;
if not(Assigned(Target)) then Exit;
if Steps < 1 then Exit;
// Store orginal poisition and size
oX := TheOBject.Left;
oY := TheOBject.Top;
oW := TheOBject.Width;
oH := TheOBject.Height;
// Position and size difference
dX := Target.Left - TheOBject.Left;
dY := Target.Top - TheOBject.Top;
dW := Target.Width - TheOBject.Width;
dH := Target.Height - TheOBject.Height;
// Animate
for i := 1 to Steps -1 do
begin
TheOBject.Left := oX + Round(i * dx / Steps);
TheOBject.Top := oY + Round(i * dY / Steps);
TheOBject.Width := oW + Round(i * dW / Steps);
TheOBject.Height := oH + Round(i * dH / Steps);
Application.ProcessMessages;
Sleep(30);
end;
// Precise position and size
TheOBject.SetBounds(Target.Left, Target.Top, Target.Width, Target.Height);
end;
end.
Basically it animates moving and zooming a control, but its not too smooth. Any ideas?
Maybe, precision issue. You used integer division, which may decrease the precision. Possible solutions: use real variables or do the calculation inside the loop like what I did in the code above.