//Declaration in TForm:
private
HG: TBitmap;
FRegion: THandle;
function CreateRegion(Bmp: TBitmap): THandle;
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
function TfrmMain.CreateRegion(Bmp: TBitmap): THandle;
var
X, Y, StartX: Integer;
Excl: THandle;
Row: PRGBArray;
TransparentColor: TRGBTriple;
begin
Bmp.PixelFormat := pf24Bit;
Result := CreateRectRGN(0, 0, Bmp.Width, Bmp.Height);
for Y := 0
to Bmp.Height - 1
do
begin
Row := Bmp.Scanline[Y];
StartX := -1;
if Y = 0
then
TransparentColor := Row[0];
for X := 0
to Bmp.Width - 1
do
begin
if (Row[X].rgbtRed = TransparentColor.rgbtRed)
and
(Row[X].rgbtGreen = TransparentColor.rgbtGreen)
and
(Row[X].rgbtBlue = TransparentColor.rgbtBlue)
then
begin
if StartX = -1
then StartX := X;
end
else
begin
if StartX > -1
then
begin
Excl := CreateRectRGN(StartX, Y, X + 1, Y + 1);
try
CombineRGN(Result, Result, Excl, RGN_DIFF);
StartX := -1;
finally
DeleteObject(Excl);
end;
end;
end;
end;
if StartX > -1
then
begin
Excl := CreateRectRGN(StartX, Y, Bmp.Width, Y + 1);
try
CombineRGN(Result, Result, Excl, RGN_DIFF);
finally
DeleteObject(Excl);
end;
end;
end;
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
DoubleBuffered:=True;
HG:=TBitmap.Create;
HG.LoadFromFile('
dein Pfad und dein Bild.bmp');
//Breite und Höhe den Bildmaßen anpassen
ClientWidth:=HG.Width;
ClientHeight:=HG.Height;
//erzeugen einer Region, als Transparentfarbe wird die Farbe des ersten Picels oben links verwendet
FRegion := CreateRegion(HG);
SetWindowRGN(
Handle, FRegion, True);
.....
end;
procedure TfrmMain.FormDestroy(Sender: TObject);
begin
HG.Free;
end;
procedure TfrmMain.FormPaint(Sender: TObject);
begin
BitBlt(Canvas.Handle,0,0,ClientWidth,ClientHeight,HG.Canvas.Handle,0,0,SRCCOPY);
end;
//so kann man ein Form einem Bitmap anpassen