Double buffering in delphi

Using Delphi XE2, I wanted some buttons to move in the delphi application.

I wrote this code:

procedure TForm1.DoSomething; var x : integer; begin for x := 200 downto 139 do begin // move two buttons Button1.Top := x; Button3.Top := x; // skip some repaints to reduce flickering if (x mod 7 = 1) then begin Form1.Repaint; Sleep(50); end; end; 

Unfortunately, it still flickers a lot during this procedure.

Here is my question: Is there a way to make the animation smooth (without any flicker)?

Edit: To make the animation smoother, change the value of 50 to a smaller one in a dream (50) and delete this line:

 if(x mod 7 = 1) then begin 
+4
source share
2 answers

Set Form1.DoubleBuffered to True . You can do this in code, but I think the property is published in XE2, so you can also set it in the Object Inspector.

+3
source

I found it better to decide how long you want the movement to be taken instead of using the sleep procedure. This is better tuned for computers with different speeds, and also tuned to move different distances. If you want it to move across the screen for 1 second, you need to move in smaller steps between the repeaters, but only 5 seconds to move around the screen.

I don’t remember exactly why, but we also added code to recolor the parent. I think we had problems displaying a ghostly image as our object moved around the screen.

Here is the code we use. It is inside a component that can switch itself on and off the screen.

 procedure TMyObject.ShiftRight; var TicksStart: int64; StartLeftValue: integer; EndLeftValue: integer; NewLeftValue: integer; LeftValueDif: integer; RemainingTicks: int64; begin StartLeftValue := Self.Left; EndLeftValue := Self.Left + Self.Width; LeftValueDif := EndLeftValue - StartLeftValue; TicksStart := GetTickCount(); RemainingTicks := FadeTime; // Fade Time is a constants that dermines how long the // slide off the screen should take while RemainingTicks > 0 do begin NewLeftValue := (LeftValueDif * (FadeTime - RemainingTicks)) div FadeTime; Self.Left := Max(StartLeftValue, NewLeftValue); Self.Parent.Repaint; Self.Repaint; RemainingTicks := FadeTime - int64(GetTickCount - TicksStart); end; if Self.Left < EndLeftValue then Self.Left := EndLeftValue; Self.Parent.Repaint; Self.Repaint; end; 
+2
source

All Articles