如何在delphi中使TImage透明?

时间:2012-08-03 13:00:32

标签: delphi

我制作了一个透明表格的小EXE,上面有一个TImage。为了使我的表单透明,我使用此代码:

Function TForm1.CombineRegions (FrmX , FrmY  :Integer;CtrlComp : TControl;Var RegHandle : THandle) : Boolean;

Var
CtrlHandle : THandle;
 CtrlLeft,
 CtrlTop,
 CtrlWidth,
 CtrlHt     : Integer;
begin
  Result := False;
  CtrlHandle := 0;
  FrmX := 0;
  FrmY := 0;
  Try
      CtrlLeft   := CtrlComp.Left;
      CtrlTop    := CtrlComp.Top;
      CtrlWidth  := CtrlComp.Width;
      CtrlHt     := CtrlComp.Height;

 Except
     Exit;
 End;

 Try
   FrmX:=0;
   FrmY:=0;
   FrmX := FrmX + CtrlLeft;
   FrmY := FrmY + CtrlTop;
   CtrlHandle := CreateRectRgn(FrmX, FrmY, FrmX + CtrlWidth, FrmY + CtrlHt) ;
   CombineRgn(RegHandle, RegHandle, CtrlHandle, RGN_OR) ;
Except
End;
End;

它的作用是首先使所有形式消失,然后根据我想要的形式控制,我将调用上述函数,并且只绘制该区域。现在我的TImage有一个图像,它有一些背景颜色。 the image

如您所见,图像有一些背景知识。我希望我的TImage被绘制,以便只绘制内部的位图,而不是整个区域。可以吗? 在此先感谢。

1 个答案:

答案 0 :(得分:0)

我有一个启动表单,只显示在屏幕上混合的32位PNG图像,我希望它可能会有所帮助。

unit SplashU;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, pngimage, ExtCtrls;

type
  TSplashForm = class(TForm)
    SplashImg: TImage;
  public
    procedure Execute;
  end;

var
  SplashForm: TSplashForm;

implementation

{$R *.dfm}

procedure TSplashForm.Execute;
var
  t: Cardinal;
  f: TBlendFunction;
  p: TPoint;
  z: TSize;
  s: DWORD;
  b: TBitmap;
  x, y:integer;
  a, d,c: PByteArray;
begin
s := GetWindowLongA(Handle, GWL_EXSTYLE);
if (s and WS_EX_LAYERED = 0) then
  SetWindowLong(Handle, GWL_EXSTYLE, s or WS_EX_LAYERED);

b := TBitmap.Create;
b.Width :=  SplashImg.Width;
b.Height :=  SplashImg.Height;
b.PixelFormat := pf32bit;
for y := SplashImg.Height - 1 downto 0 do
  begin
  a := (SplashImg.Picture.Graphic as TPNGObject).AlphaScanline[y];
  c := (SplashImg.Picture.Graphic as TPNGObject).Scanline[y];
  d := b.ScanLine[y];
  for x := 0 to SplashImg.Width - 1 do
    begin
    d^[x * 4 + 3] := a^[x];
    d^[x * 4] := MulDiv(c^[x * 3], a^[x], 255);
    d^[x * 4 + 1] := MulDiv(c^[x * 3 + 1], a^[x], 255);
    d^[x * 4 + 2] := MulDiv(c^[x * 3 + 2], a^[x], 255);
    end;
  end;

p := Point(0, 0);
z.cx := b.Width;
z.cy := b.Height;

f.BlendOp := AC_SRC_OVER;
f.BlendFlags := 0;
f.SourceConstantAlpha := 0;
f.AlphaFormat := AC_SRC_ALPHA;

Show;
t := 0;
while (f.SourceConstantAlpha < 255) do
  begin
  while (t = GetTickCount) do Sleep(25);
  t := GetTickCount;
  Inc(f.SourceConstantAlpha, (255 - f.SourceConstantAlpha) div 32 + 1);
  UpdateLayeredWindow(Handle, 0, nil, @z, b.Canvas.Handle, @p, 0, @f, ULW_ALPHA);
  end;
b.Free;
end;

end.