What is the simplest way to add an AlphaBlendValue property to Delphi's TImage
I have a "caution" image on a dialog that is shown if there are questionable parameter values. Users do not always notice it, so I want to fade it in and out cyclically over a second or so (yes, I could just toggle the Visible property, but that would look a bit like I was just toggling the Visible property). Is there a simpler way 开发者_Python百科than putting it on it's own form and floating it over the dialog (and changing the AlphaBlendValue property of the form)?
You can do this using the Opacity
parameter of TCanvas.Draw
. Behind the scenes this calls TGraphic.DrawTransparent
which in turn calls the Windows AlphaBlend
API function. An easy way to implement this is with a TPaintBox
:
procedure TAlphaBlendForm.FormCreate(Sender: TObject);
begin
FBitmap := TBitmap.Create;
FBitmap.Assign(Image1.Picture.Graphic);//Image1 contains a transparent PNG
PaintBox1.Width := FBitmap.Width;
PaintBox1.Height := FBitmap.Height;
Timer1.Interval := 20;
end;
procedure TAlphaBlendForm.PaintBox1Paint(Sender: TObject);
begin
PaintBox1.Canvas.Draw(0, 0, FBitmap, FOpacity);
end;
procedure TAlphaBlendForm.Timer1Timer(Sender: TObject);
begin
FOpacity:= (FOpacity+1) mod 256;
PaintBox1.Invalidate;
end;
If you are using an older version of Delphi without the Opacity
parameter of TCanvas.Draw
you can use AlphaBlend
directly.
procedure TAlphaBlendForm.PaintBox1Paint(Sender: TObject);
var
fn: TBlendFunction;
begin
fn.BlendOp := AC_SRC_OVER;
fn.BlendFlags := 0;
fn.SourceConstantAlpha := FOpacity;
fn.AlphaFormat := AC_SRC_ALPHA;
Windows.AlphaBlend(
PaintBox1.Canvas.Handle,
0,
0,
PaintBox1.Width,
PaintBox1.Height,
FBitmap.Canvas.Handle,
0,
0,
FBitmap.Width,
FBitmap.Height,
fn
);
end;
Thanks to Giel for suggesting the Opacity
parameter of TCanvas.Draw
, and for Sertac for pointing out that it is quite a recent addition to TCanvas.Draw
.
TImage does not suppor alpha transparency like you are looking for. Using a separate floating TForm is the simpliest option.
精彩评论