Эта функция основана на идее Дэйва Элсберри.
Что отличается:
- Прозрачно рисует только тень
- Он использует почти в 2 раза меньше оперативной памяти
- Параметры
procedure DrawShadowText(aCanvas: TCanvas; CONST Text: string; CONST X, Y, Opacity: Integer; TextColor, ShadowColor: TColor);
{ Opacity a value from 0-255:
$00 is completely transparent,
$FF is completely opaque.
$C0 is 75% opaque }
CONST ShadowSize= 1;
VAR
TempBMP: TBitmap;
BlendFunc: BLENDFUNCTION;
H, W: Integer;
begin
BlendFunc.BlendOp := AC_SRC_OVER;
BlendFunc.BlendFlags := 0;
BlendFunc.SourceConstantAlpha := Opacity;
BlendFunc.AlphaFormat := AC_SRC_ALPHA;
{ Create another TBitmap to hold the text you want to overlay }
TempBMP := Graphics.TBitmap.Create;
TRY
TempBMP.Canvas.Font.Style := [fsBold];
TempBMP.Canvas.Brush.Style := bsClear;
W:= TempBMP.Canvas.TextWidth(Text);
H:= TempBMP.Canvas.TextHeight(Text);
TempBMP.SetSize(W+ShadowSize, H+ShadowSize);
{ In AlphaBlend, Black is always 100% transparent. So, paint TempBMP completely Black. }
TempBMP.Canvas.Brush.Color := clBlack;
TempBMP.Canvas.FloodFill(0, 0, clNone, fsBorder);
{ Write the shadow first }
TempBMP.Canvas.Font.Color := ShadowColor;
TempBMP.Canvas.TextOut(ShadowSize, ShadowSize, Text); { Diagonal left shadow }
TempBMP.Canvas.TextOut(ShadowSize, 0, Text); { Left shadow }
{ Draw the text with transparency:
TempBMP.Canvas.Brush.Style := bsClear;
TempBMP.Canvas.Font.Color := TextColor;
TempBMP.Canvas.TextOut(0, 0, Text); }
{ Use the AlphaBlend function to overlay the bitmap holding the text on top of the bitmap holding the original image. }
Windows.AlphaBlend(aCanvas.Handle,
x, y, TempBMP.Width, TempBMP.Height,
TempBMP.Canvas.Handle, 0, 0, TempBMP.Width, TempBMP.Height,
BlendFunc);
{ Draw the text at 100% opacity }
aCanvas.Font.Style := [fsBold];
aCanvas.Brush.Style := bsClear;
aCanvas.Font.Color := TextColor;
aCanvas.TextOut(x, y-1, Text);
FINALLY
FreeAndNil(TempBMP);
END;
end;
procedure TfrmTest.UseIt;
VAR BackgroundImage: tbitmap;
begin
BackgroundImage := Graphics.TBitmap.Create;
try
BackgroundImage.LoadFromFile('c:\test.bmp');
DrawShadowText (BackgroundImage.Canvas, 'This is some demo text', 20, 40, 140, clRed, clSilver);
Image1.Picture.Bitmap.Assign(BackgroundImage);
FINALLY
BackgroundImage.Free;
end;
end;