Title: How to make a form that`s shaped like a bitmap image (scanline)
Question: How to make a form that's shaped like the image on a bitmap.
Answer:
The following example shows how you can create a form shaped
like the image in a bitmap. The image in the bitmap must contain
colors other than white, the background must be white, because
this is going to be filtered out.
Example
-------
Global Variables
----------------
(In The Private Section Of Your Form Declaration)
SourceBitmap : TBitmap;
Procedures
----------
(In The Public Section Of Your Form Declaration)
procedure GetColor(const SLine : PByteArray; const L: Integer;
,var R: Integer,var C: TColor);
function GetColorOf(ScanLine: PByteArray; X : Word): TColor;
Int The OnCreate Event Of The Form You Put
------------------------------------------
var
NewRgn, RowRgn, ScanRgn : HRGN;
Rows, Left, Right : Integer;
Line : PByteArray;
SourceColor : TColor;
begin
SourceBitmap := TBitmap.Create;
SourceBitmap.LoadFromFile('example.bmp');
NewRgn := CreateRectRgn(0,0,0,0);
For Rows := 0 To SourceBitmap.Height - 1 Do
Begin
RowRgn := CreateRectRgn(0,Rows,0,Rows);
Line := SourceBitmap.Scanline[Rows];
Left := 0; Right := 0;
repeat
GetColor(Line,Left,Right,SourceColor);
If (SourceColor clWhite) Then
Begin
ScanRgn := CreateRectRgn(Left,Rows,Right + 1,Rows + 1);
CombineRgn(RowRgn,RowRgn,ScanRgn,RGN_OR);
DeleteObject(ScanRgn);
End;
CombineRgn(NewRgn,NewRgn,RowRgn,RGN_OR);
DeleteObject(RowRgn);
while (right = SourceBitmap.Width);
End;
SetWindowRgn(Handle,NewRgn,True);
end;
In The GetColor Procedure
-------------------------
var
Column : integer;
CompareColor : TColor;
begin
Column := L;
CompareColor := GetColorOf(SLine,L);
repeat
Inc(Column);
until (CompareColor GetColorOf(SLine,Column))
or (Column = SourceBitmap.Width);
R := column - 1;
C := CompareColor;
end;
In The Function GetColorOf
--------------------------
var
Red, Green, Blue : Byte;
begin
If SourceBitmap = nil then exit;
If (X SourceBitmap.Width) then Exit;
Result := clBlack;
Red := ScanLine[X * 3];
Green := ScanLine[X * 3 + 1];
Blue := ScanLine[X * 3 + 2];
If ((Red = 138) and (Green = 138) and (Blue = 138)) Then
Result := clWhite
end;