const {An array of points for the star region} RgnPoints:array[1..10] of TPoint= ((x:203;y:22),(x:157;y:168),(x:3;y:168),(x:128;y:257), (x:81;y:402),(x:203;y:334),(x:325;y:422),(x:278;y:257), (x:402;y:168),(x:249;y:168));//确定顶点 LinePoints:array[1..11] of Tpoint= ((x:199;y:0),(x:154;y:146),(x:2;y:146),(x:127;y:235), (x:79;y:377),(x:198;y:308),(x:320;Y:396),(x:272;y:234), (x:396;y:146),(x:244;y:146),(x:199;Y:0));{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject); var Rgn:HRGN; begin Setwindowpos(Form1.Handle,HWND_TOPMOST,Form1.Left,form1.Top,Form1.Width,Form1.Height,0); Rgn:=CreatepolygonRgn(Rgnpoints,High(RgnPoints),ALTERNATE); SetWindowRgn(Handle,rgn,True); Form1.color:=clgreen; end;
procedure tform1.formcreate(sender:tobject); var hr:thandel; small:integer; begin if width>=height then small:=height else small:=width; hr:=createellipticrgn(0,0,small,small); setwidowrgn(handle,hr,true); end;
你可以找delphi设计灵感其中有这样的代码。
procedure TForm1.FormClick(Sender: TObject); var R : HRgn; begin if IsRound then begin SetWindowRgn(Handle,0,True); DeleteObject(R); IsRound := False; end else begin R := CreateEllipticRgn(-15,-15,Width+15,Height+15); SetWindowRgn(Handle,R,True); IsRound := True; end; end;
procedure TForm1.FormCreate(Sender: TObject); begin IsRound := False; end;
{An array of points for the star region}
RgnPoints:array[1..10] of TPoint=
((x:203;y:22),(x:157;y:168),(x:3;y:168),(x:128;y:257),
(x:81;y:402),(x:203;y:334),(x:325;y:422),(x:278;y:257),
(x:402;y:168),(x:249;y:168));//确定顶点
LinePoints:array[1..11] of Tpoint=
((x:199;y:0),(x:154;y:146),(x:2;y:146),(x:127;y:235),
(x:79;y:377),(x:198;y:308),(x:320;Y:396),(x:272;y:234),
(x:396;y:146),(x:244;y:146),(x:199;Y:0));{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
var Rgn:HRGN;
begin
Setwindowpos(Form1.Handle,HWND_TOPMOST,Form1.Left,form1.Top,Form1.Width,Form1.Height,0);
Rgn:=CreatepolygonRgn(Rgnpoints,High(RgnPoints),ALTERNATE);
SetWindowRgn(Handle,rgn,True);
Form1.color:=clgreen;
end;
var
hr:thandel;
small:integer;
begin
if width>=height then
small:=height
else
small:=width;
hr:=createellipticrgn(0,0,small,small);
setwidowrgn(handle,hr,true);
end;
var
R : HRgn;
begin
if IsRound then
begin
SetWindowRgn(Handle,0,True);
DeleteObject(R);
IsRound := False;
end
else
begin
R := CreateEllipticRgn(-15,-15,Width+15,Height+15); SetWindowRgn(Handle,R,True);
IsRound := True;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
IsRound := False;
end;
end.
////////////////////////////////////////////
四四方方的窗体也会变形,利用win32 API函数setwindowrgn就可以将窗口定义为任何形状,以下是将窗口定义为园角矩形的例子:
procedure TPortForm.FormCreate(Sender: Tobject);
var hr :thandle;
begin
hr:=createroundrectrgn(0,0,width,height,20,20);//定义园角矩形(win API函数)
setwindowrgn(handle,hr,true); //设置园角窗口
end;
为了使该窗口更好,应在onResize事件处理程序放相同的代码。
/////////////////////////////////////
用汉字做窗体形状
procedure TForm1.FormCreate(Sender: TObject);
var
rgn:HRGN;
begin
BeginPath(Canvas.Handle);
SetBkMode( Canvas.Handle, TRANSPARENT );
Canvas.Font.Name:= '宋体';
Canvas.Font.Size:=100;
Canvas.TextOut( 20, 20, '漂亮吗?');//用"漂亮吗?"作为form的形状
EndPath(Canvas.Handle);
rgn:= PathToRegion(Canvas.Handle);
SetWindowRgn( Handle, rgn, true );
end;