2012-08-03 541 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.