2
votes

I have made a small EXE with a transparent form which has a TImage on it . For making my form transparent , i use this code :

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;

What it does is first make all the form disappear , then as per which controls i want on form , i will call the above function and only that region will be drawn . Now my TImage has an image which has some background color to it. the image

As you can see , the image has some background to it . I want my TImage to be drawn so that only the bitmap inside is drawn , not the whole region. Can it be done ? Thanks in Advance.

1
So you want to have a transparent form with the only picture visible on it, right ?TLama
ah yes , that is more or less what i wantCyprUS
Do you need a TImage or would be enough for you when the picture would be loaded just from file and displayed on a transparent form ?TLama
a TImage would be nicer .but then , i could not really careCyprUS

1 Answers

0
votes

I have a splash form that only shows a 32bit PNG image blended on the screen, I hope it could be helpful.

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.