用API函数CreatePolyRgn就可以搞定
下边给个例子, 该窗体中有一个image4, 和三个按钮。其它没有
unit PolygonRgn;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TFormPolygon = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Image4: TImage;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Image4MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
myPolygonRgn:hrgn;
end;
var
FormPolygon: TFormPolygon;
implementation
{$R *.DFM}
procedure TFormPolygon.Button1Click(Sender: TObject);
var
i:integer;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;
TP = array of TPoint;
var
P: PPoints;
pary: TP;
function CreatePolyRgn(var Points: TP): HRgn;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;
begin
Result := CreatePolygonRgn(PPoints(Points)^, High(Points) + 1, WINDING);
end;
begin
Image4.canvas.brush.color:=clred;
setlength(pary,100);
pary[0]:=point(20,10);
pary[1]:=point(180,30);
pary[2]:=point(180,180);
pary[3]:=point(0,180);
pary[4]:=point(120,180);
pary[5]:=point(55,130);
try
myPolygonRgn:=CreatePolyRgn(Pary);
if myPolygonRgn<>0 then
fillrgn(Image4.canvas.handle,myPolygonRgn,Image4.canvas.brush.handle)
else
showmessage('invalid region');
finally
// deleteobject(myPolygonRgn);
end;
end;
procedure TFormPolygon.Button2Click(Sender: TObject);
var pary:array of tpoint;
i:integer;
myPolygonRgn:hrgn;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;
var
P: PPoints;
// Copy From RXHints.pas
function CreatePolyRgn(const Points: array of TPoint): HRgn;
type
PPoints = ^TPoints;
TPoints = array[0..0] of TPoint;
begin
Result := CreatePolygonRgn(PPoints(@Points)^, High(Points) + 1, WINDING);
end;
begin
Image4.canvas.brush.color:=clBlue;
setlength(pary,4);
pary[0]:=point(0,0);
pary[1]:=point(180,0);
pary[2]:=point(180,180);
pary[3]:=point(0,180);
try
myPolygonRgn:=CreatePolyRgn(Pary);
if myPolygonRgn<>0 then
fillrgn(Image4.canvas.handle,myPolygonRgn,Image4.canvas.brush.handle)
else
showmessage('invalid region');
finally
deleteobject(myPolygonRgn);
end;
end;
procedure TFormPolygon.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ptInRegion(myPolygonRgn,X,Y)=false then
caption:='不在多边形内'
else
caption:='在多边形内!!!!!!1';
end;
procedure TFormPolygon.Image4MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
if ptInRegion(myPolygonRgn,X,Y)=false then
caption:='不在多边形内'
else
caption:='在多边形内!!!!!!1';
end;
end.