我用透明的表格制作了一个小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的图像带有一些背景色。

如您所见,图像具有一定的背景。我希望绘制TImage,以便仅绘制内部的位图,而不绘制整个区域。能做到吗?
提前致谢。

最佳答案

我有一个初始表格,仅显示在屏幕上混合的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.

关于delphi - 如何使TImage在Delphi中透明?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11796228/

10-10 17:06