2012-08-03 545 views
2

我已经做了一个小的EXE透明窗体,它有一个TImage。为了使我的形式透明,我用这个代码:如何在delphi中使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能够被绘制出来,以便只绘制里面的位图,而不是整个区域。可以做到吗? 在此先感谢。

+0

所以你想要一个透明的窗体,只有图片可见,对不对? – TLama 2012-08-03 13:04:42

+0

啊是的,这或多或少是我想要的 – CyprUS 2012-08-03 13:06:40

+0

当图片只是从文件加载并显示在透明表单上时,您是否需要一个'TImage'或对于您来说就足够了? – TLama 2012-08-03 13:18:35

回答

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.