1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| unit Sprites;
interface
uses
Windows, SysUtils,Classes,Graphics,Controls,ExtCtrls;
type
TSpriteOrigin = (so00,so10,so11,so01); //correspond au 4 point cartesient
TTextSprite = Class
private
FSpriteOrigin : TSpriteOrigin; //origine du point x,y en bas a droit, en haut a gauche etc...
FActive : Boolean;
RBG : TRect; //Rect BackCround
BG : TBitMap; //BackGround
Target : TCanvas; // TCanvas
public
W,H : Integer; //Dimention de l'espace que prend le text
RFG : Trect; //Rect ForeGround
FG : TBitMap; //ForGround
Constructor Create(Destination:TCanvas);
Destructor Destroy;override;
procedure Show(x, y: Integer;T:String);
procedure Hide;
Procedure DoActivate(Rep:Boolean);
procedure SetSpriteOrigin(Origin: TSpriteOrigin);
published
End;
implementation
Procedure TTextSprite.SetSpriteOrigin(Origin:TSpriteOrigin);
Begin
FSpriteOrigin := Origin;
End;
Procedure TTextSprite.DoActivate(Rep:Boolean);
Begin
if rep = false then begin
rbg:= RECT(-100,-100,1,1);
rfg:= RECT(-100,-100,1,1);
end;
FActive := Rep;
End;
Constructor TTextSprite.Create(Destination:TCanvas);
Begin
Target:=Destination;
FG := TBitMap.Create;
BG := TBitmap.Create;
fg.Canvas.Font.name := Destination.Font.name;
fg.Canvas.Font.size := Destination.Font.Size;
fg.Canvas.Font.Color := Destination.Font.Color;
fg.Canvas.Brush.Color := Destination.Brush.Color;
fg.Canvas.Pen.Color := Destination.Pen.Color;
FActive := true;
FSpriteOrigin := so00; //Par default set origine en haut a gauche (normale)
End;
Destructor TTextSprite.Destroy;
begin
FG.Free;
BG.Free;
inherited;
end;
Procedure TTextSprite.Show(x,y:Integer;T:String);
Begin
if not Factive then exit;
W := FG.Canvas.TextWidth(T)+2;
H := FG.Canvas.TextHeight(T)+2;
case FSpriteOrigin of
so01: Y := Y - H;
so10: X := X - W;
so11:
Begin
Y := Y - H;
X := X - W;
End;
end;
RBG := rect(x,y,x+w,y+h);
RFG := RECT(0,0,w,h);
bg.Width := w;
bg.Height := h;
bg.Canvas.CopyRect(RFG,Target,RBG); //Memorise l'image avant de dessiner
FG.Width :=w;
FG.Height :=h;
fg.canvas.Brush.Color := $f0f0f0;//clltgray;
fg.Canvas.TextRect(RFG,0,0,t);
Target.CopyRect(RBG,fg.Canvas ,RFG);
End;
Procedure TTextSprite.Hide;
begin
if FActive then
Target.CopyRect(RBG,bg.Canvas ,rfg); //REPLACE L'IMAGE TARGET
end;
end.
{
exemple d'utilisation
code Pour bouger le TextSprite
Dans create form..
1-ts:=TTextSprite.Create(PaintBox1);
ts.fg.Canvas.Brush.Color:=rclred;
...
Dans mouseMove
ts.Hide; //Replacer l'image arriere
ts.Show(x,y,'Coor :'+ Inttostr(x) +','+ inttostr(y)); //Memorise l'image arriere et affiche le TextSprite devant Limage arriere
} |
Partager