PDA

View Full Version : Fade in and out (Delphi Form)



Wizard
29-04-2009, 04:29 PM
Fade in and out teqnique - Delphi

Ever wondered how to achieve a fade effect with a Delphi form? You can impress your clients and/or friends with a sleek fade-in effect when your application starts and a fade-out effect when the application close. One way to do it is to make use of the form’s AlphaBlendValue.

Set AlphaBlendValue (in the form’s object inspector) to a value between 0 and 255 to indicate the degree of translucency when the AlphaBlend property is true. A value of 0 indicates a completely transparent window and a value of 255 indicates complete opacity. We will set the AlphaBlendValue to zero and then increase and decrease the value using the timers on our form. Remember that AlphaBlendValue only has an effect when the AlphaBlend property is true.

The following steps shows how to fade a Delphi form in and out:

Step 1: Start a new application in Delphi
Step 2: Put two timers and a button on a form
Step 3: Set AlphaBlend to True in form's object inspector
Step 4: Set AlphaBlendValue to 0 in form's object inspector
Step 5: Set Timer1.enabled to true
Step 6: Set Timer2.enabled to false
Step 7: Set the interval of both timers to 10

Step 8: Place the following code in the Timer1.onTimerEvent:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
if Form1.AlphaBlendValue >= 255 then
Timer1.Enabled := false
else
Form1.AlphaBlendValue := Form1.AlphaBlendValue + 5;
end;


Step 9: Place the following code in the Timer2.onTimerEvent :

procedure TForm1.Timer2Timer(Sender: TObject);
begin
if Form1.AlphaBlendValue <= 0 then
Timer2.Enabled := false
else
Form1.AlphaBlendValue := Form1.AlphaBlendValue - 5;
if Form1.AlphaBlendValue <= 0 then
close;
end;


Step 10: Place the following code in the onClick even of the button:

Timer2.Enabled := True;


Step 11: Click run or hit F9 in Delphi and compile. You can now change the forms’ color and experiment further.