Author: Tomas Rutkauskas
How to fade text in and out on a TCanvas
Answer:
1
2 function TFadeEffect.FadeInText(Target: TCanvas; X, Y: integer; FText: string):
3 TRect;
4 var
5 Pic: TBitmap;
6 W, H: integer;
7 PicRect, TarRect: TRect;
8 begin
9 Pic := TBitmap.Create;
10 Pic.Canvas.Font := Target.Font;
11 W := Pic.Canvas.TextWidth(FText);
12 H := Pic.Canvas.TextHeight(FText);
13 Pic.Width := W;
14 Pic.Height := H;
15 PicRect := Rect(0, 0, W, H);
16 TarRect := Rect(X, Y, X + W, Y + H);
17 Pic.Canvas.CopyRect(PicRect, Target, TarRect);
18 SetBkMode(Pic.Canvas.Handle, Transparent);
19 Pic.Canvas.TextOut(0, 0, FText);
20 FadeInto(Target, X, Y, Pic);
21 Pic.Free;
22 FadeInText := TarRect;
23 end;
24
25 procedure TFadeEffect.FadeOutText(Target: TCanvas; TarRect: TRect; Orig: TBitmap);
26 var
27 Pic: TBitmap;
28 PicRect: TRect;
29 begin
30 Pic := TBitmap.Create;
31 Pic.Width := TarRect.Right - TarRect.Left;
32 Pic.Height := TarRect.Bottom - TarRect.Top;
33 PicRect := Rect(0, 0, Pic.Width, Pic.Height);
34 Pic.Canvas.CopyRect(PicRect, Orig.Canvas, TarRect);
35 FadeInto(Target, TarRect.Left, TarRect.Top, Pic);
36 Pic.Free;
37 end;
|