(*//
標題:取得圖片的透明區域
說明:適用於制作復雜的不規則窗體
設計:Zswang
日期:2004-03-10
//*)
(*//============================================================================
設計思路:~~
就是對畫布一行一行的掃描~~
對於不是透明色相連的像素都看成一條條的線段~~
───────── ─ ─── ─────
─────── ──── ───────
─── ─── ── ───────
── ─────────
用這些線段組合成不規則的區域~~
線段就是找到開始位置和結束位置就行了~~
組合區域是最花時間的地方~~
減少組合區域的頻率就可以提高運行的速度~~
用線段組合就比用點組合少多了~~
============================================================================//*)
function GraphicToRGN(mGraphic: TGraphic; mTransPoint: TPoint): HRGN;
var
I, J: Integer;
vStart: Integer;
vHandle: HRGN;
vTransColor: TColor;
begin
Result := 0;
if not Assigned(mGraphic) then Exit;
Result := CreateRectRgn(0, 0, 0, 0);
with TBitmap.Create do try
Width := mGraphic.Width;
Height := mGraphic.Height;
Canvas.Draw(0, 0, mGraphic);
vTransColor := Canvas.Pixels[mTransPoint.X, mTransPoint.Y];
for I := 0 to Height - 1 do begin
vStart := 0;
for J := 0 to Width do begin
if (Canvas.Pixels[J, I] <> vTransColor) and (J < Width) then
if vStart < 0 then
vStart := J
else
else if vStart >= 0 then begin
vHandle := CreateRectRgn(vStart, I, J, I + 1);
try
CombineRgn(Result, Result, vHandle, RGN_OR);
finally
DeleteObject(vHandle);
end;
vStart := -1;
end;
end;
end;
finally
Free;
end;
end; { GraphicToRGN }
//Example
procedure TForm1.Button1Click(Sender: TObject);
var
vRGN: HRGN;
begin
BorderStyle := bsNone;
Image1.Left := 0;
Image1.Top := 0;
vRGN := GraphicToRGN(Image1.Picture.Graphic, Point(0, 0));
try
SetWindowRgn(Handle, vRGN, True);
finally
DeleteObject(vRGN);
end;
end;