views:

222

answers:

2

I want my form slide down and back to position with slide animation, how to make correct AnimateWinows, if it real for sure ...

void __fastcall TUsers::BitBtn1Click(TObject *Sender)
{
if (!pressed)
{
        Height=700;
        //AnimateWindow(Handle, 500, AW_CENTER | AW_SLIDE | AW_VER_POSITIVE);
        pressed=true;
}
else
{
        pressed=false;
        //AnimateWindow(Handle, 500, AW_CENTER | AW_SLIDE | AW_VER_NEGATIVE);
        Height=425;
}
}

to moderators : here is no mutters Builder or Delphi :)

+1  A: 

That's not what AnimateWindow is for. That function hides or shows a window using some animation. Your window is already visible, and you want it to stay visible, so AnimateWindow is not for you.

What you should do instead is make your window successively taller or shorter in a loop until you reach the new desired height.

Rob Kennedy
Hm... I think I sow some example when AnimateWindow works with a frames... I think I need to separate somehow my form for a two frames and then use slide somehow.
nCdy
Unfortunately AnimateWindow works only with windows (aka TForms), not with child windows (aka TFrames and TControls).
Alex T.
+2  A: 

If you are interested in controlling the animation yourself, here is a sample of code we wrote to accomplish that. It looks and works great. We are sliding Tform1 from the right to the left within a TPanel control on the main form. We ensure that Self.Parent and DoubleBuffered gets set correctly in MyCreate. ShiftLeft and then ShiftRight do the work. We ran into a problem for certain users where the Self.Top was being shifted so we are making sure that Self.Top := 0 with each iteration and when fully shifted. That solved all of the weird issues we were seeing.

Hope this helps!

{
  TForm1.MyCreate
  ---------------------------------------------------------------------------
}
constructor TForm1.MyCreate(AOwner: TComponent);
var
  OwnerControl: TWinControl;
begin
  inherited Create(AOwner);

  if Owner is TWinControl then
  begin
    OwnerControl := Owner as TWinControl;
    Self.Parent := OwnerControl;
  end;

  Self.Visible := false;
  Self.DoubleBuffered := true;
  Self.BorderStyle := bsNone;

end;

{
  TForm1.ShiftLeft
  ---------------------------------------------------------------------------
}
procedure TForm1.ShiftLeft;
var
  TicksStart: int64;
  InitLeftValue: integer;
  StartLeftValue: integer;
  NewLeftValue: integer;
  LeftValueDif: integer;
  RemainingTicks: int64;

begin
  Self.Top := 0;
  Self.Height := Self.Parent.ClientHeight;
  Self.Width := Self.Parent.ClientWidth;

  InitLeftValue := Self.Parent.Left;
  StartLeftValue := Self.Parent.Left + Self.Parent.ClientWidth;
  LeftValueDif := StartLeftValue - InitLeftValue;

  Self.Left := StartLeftValue;
  Self.Visible := true;

  TicksStart := GetTickCount();
  RemainingTicks := FadeTime;

  while RemainingTicks > 0 do
  begin
    NewLeftValue := (LeftValueDif * RemainingTicks) div FadeTime;
    Self.Left := Max(InitLeftValue, NewLeftValue);
    Self.Parent.Repaint;
    Self.Top := 0;
    Self.Repaint;

    RemainingTicks := FadeTime - int64(GetTickCount - TicksStart);
  end;

  if Self.Left > InitLeftValue then
    Self.Left := InitLeftValue;

  Self.Parent.Repaint;
  Self.Top := 0;
  Self.Repaint;
end;

{
  TForm1.ShiftRight
  ---------------------------------------------------------------------------
}
procedure TForm1.ShiftRight;
var
  TicksStart: int64;
  StartLeftValue: integer;
  EndLeftValue: integer;
  NewLeftValue: integer;
  LeftValueDif: integer;
  RemainingTicks: int64;

begin
  Self.Top := 0;
  StartLeftValue := Self.Left;
  EndLeftValue := Self.Left + Self.Width;
  LeftValueDif := EndLeftValue - StartLeftValue;

  TicksStart := GetTickCount();
  RemainingTicks := FadeTime;

  while RemainingTicks > 0 do
  begin
    NewLeftValue := (LeftValueDif * (FadeTime - RemainingTicks)) div FadeTime;
    Self.Left := Max(StartLeftValue, NewLeftValue);
    Self.Parent.Repaint;
    Self.Top := 0;
    Self.Repaint;

    RemainingTicks := FadeTime - int64(GetTickCount - TicksStart);
  end;

  if Self.Left < EndLeftValue then
    Self.Left := EndLeftValue;

  Self.Parent.Repaint;
  Self.Top := 0;
  Self.Repaint;
end;
Warren Markon
FadeTime is a constant we set to 300.
Warren Markon
thank you for a good case :)
nCdy