我来说一个简单的方案 在窗体上放置一个Image 且画一幅图把你要透明的颜色都标注出来 这样Image 设置成Alclient function GetRGN(TransClolor:TColor):Hrgn: var I,j:integer; Rgn1:Hrgn; rgn2:Hrgn; tmp:HRGN; begin rgn:=0; for i:=0 to Image.Picture.With do for j:=0 Image.Picture.Height do begin if (Image.Picture.Pix[i][j]<>TransColor) and (Image.Picture.Pix[i][j]<>TransColor) then begin rgn2:=CreateRectRGn(i,j,i+1,j+1); if CombineRGN(rgn,rgn,rgn2,RGN_OR)<>ERROR then DeleteObject(RGn2) else showMessage("errror");
end; end result:=RGN; end; procedure SetRGNToForm(Form:TForm);
begin SetWindowRGN(Form.Handle,GetRGN(Rgb(0,0,0),true); end; ALL IS ok!
这个算法可以,不使用Image也可以使用这种方法。只是不知道会不会慢(我现在用不了电脑)。
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormPaint(Sender: TObject); function GetRGN(AColor:TColor):HRGN; procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } public { Public declarations } end;var Form1: TForm1; ABMP:TBitmap; implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject); begin self.BorderStyle:=bsNone; ABMP:=TBitMap.Create; ABMP.LoadFromFile('1.BMP'); ABMP.PixelFormat:=pf24bit; self.Width:=ABMP.Width; self.Height:=ABmp.Height; SetWindowRGN(Handle,GetRGN(ABMP.Canvas.Pixels[1,1]),true); end;procedure TForm1.FormDestroy(Sender: TObject); begin ABMP.Free; end;procedure TForm1.FormPaint(Sender: TObject); begin Canvas.Draw(0,0,ABMP); end;function TForm1.GetRGN(AColor:Tcolor):HRGN; Type TRGBArray=array [0..32767,0..2] of Byte; var x,y:Integer; p:^TRGBArray; r1,r2:HRGN; begin r1:=0; for y:=0 to ABMP.Height-1 do Begin p:=ABMP.ScanLine[y]; For x:=0 to ABMP.Width-1 do if rgb(p[x,2],p[x,1],p[x,0])<>AColor then Begin if r1=0 then r1:=CreateRectRgn(x,y,x+1,y+1) else Begin r2:=CreateRectRgn(x,y,x+1,y+1); CombineRGN(r1,r1,r2,RGN_OR); DeleteObject(r2); end; end; End; result:=r1; end;procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin if Button=mbleft then begin ReleaseCapture; SendMessage(handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); end else if button=mbright then close; end;end.
和并 CombineRgn,
把区域与窗口挂接setWindowRgn
OK
你的想法我第一次看到,这样是什么效果呢。是全部透明吗?怎样设置指定颜色透明。
在窗体上放置一个Image 且画一幅图把你要透明的颜色都标注出来
这样Image 设置成Alclient
function GetRGN(TransClolor:TColor):Hrgn:
var
I,j:integer;
Rgn1:Hrgn;
rgn2:Hrgn;
tmp:HRGN;
begin
rgn:=0;
for i:=0 to Image.Picture.With do
for j:=0 Image.Picture.Height do
begin
if (Image.Picture.Pix[i][j]<>TransColor) and
(Image.Picture.Pix[i][j]<>TransColor) then
begin
rgn2:=CreateRectRGn(i,j,i+1,j+1);
if CombineRGN(rgn,rgn,rgn2,RGN_OR)<>ERROR then
DeleteObject(RGn2)
else
showMessage("errror");
end;
end
result:=RGN;
end;
procedure SetRGNToForm(Form:TForm);
begin
SetWindowRGN(Form.Handle,GetRGN(Rgb(0,0,0),true);
end;
ALL IS ok!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
function GetRGN(AColor:TColor):HRGN;
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
ABMP:TBitmap;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
self.BorderStyle:=bsNone;
ABMP:=TBitMap.Create;
ABMP.LoadFromFile('1.BMP');
ABMP.PixelFormat:=pf24bit;
self.Width:=ABMP.Width;
self.Height:=ABmp.Height; SetWindowRGN(Handle,GetRGN(ABMP.Canvas.Pixels[1,1]),true);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
ABMP.Free;
end;procedure TForm1.FormPaint(Sender: TObject);
begin
Canvas.Draw(0,0,ABMP);
end;function TForm1.GetRGN(AColor:Tcolor):HRGN;
Type
TRGBArray=array [0..32767,0..2] of Byte;
var
x,y:Integer;
p:^TRGBArray;
r1,r2:HRGN;
begin
r1:=0;
for y:=0 to ABMP.Height-1 do Begin
p:=ABMP.ScanLine[y];
For x:=0 to ABMP.Width-1 do
if rgb(p[x,2],p[x,1],p[x,0])<>AColor then Begin
if r1=0 then
r1:=CreateRectRgn(x,y,x+1,y+1)
else Begin
r2:=CreateRectRgn(x,y,x+1,y+1);
CombineRGN(r1,r1,r2,RGN_OR);
DeleteObject(r2);
end;
end;
End;
result:=r1;
end;procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button=mbleft then begin
ReleaseCapture;
SendMessage(handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
end
else if button=mbright then
close;
end;end.