我的小代码以全屏尺寸创建具有不同形状的表单画布的现代艺术。 我可以使椭圆,矩形和线条起作用,但不能使polycon起作用。谁能帮我?(版本:德尔福社区版(
uses .... GDIPAPI, GDIPOBJ, GDIPUTIL;
procedure TForm1.Button1Click(Sender: TObject);
var
graphics: TGPGraphics;
SolidPen: TGPPen;
SolidBrush : TGPBrush;
x,y,x2,y2,x3,y3 : integer;
begin
graphics := TGPGraphics.Create(Canvas.Handle);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
SolidPen := TGPPen.Create(MakeColor(255, random(255), random(255), random(255)), random(4)+1);
SolidBrush := TGPSolidBrush.Create(MakeColor(255, random(255), random(255), random(255)));
SolidPen.SetStartCap(LineCapRound);
SolidPen.SetEndCap(LineCapRound);
//POLYCON, not working.------------------
// PROBLEM HERE: it's complaining: 'Oridinal type required' ,
// 'incompatible type: integer and TPoint'
x:= 150; y := 50; x2 := 50; y2 := 250; x3 := 250; y3 := 250;
graphics.FillPolygon(SolidBrush, [Point(x, y), Point(x2, y2), Point(x3, y3)]);
graphics.DrawPolygon(SolidPen, [Point(x, y), Point(x2, y2), Point(x3, y3)]);
//--------------------------------------------
// ELLIPSE, ok
x := random(Form1.width); y := random(Form1.height); x2 := random(200); y2 := random(200);
graphics.FillEllipse(SolidBrush,x, y, x2, y2);
graphics.DrawEllipse(SolidPen,x, y, x2, y2);
// RECTANGLE, ok
x := random(Form1.width); y := random(Form1.height); x2 := random(200); y2 := random(200);
graphics.FillRectangle(SolidBrush, x, y, x2, y2);
graphics.DrawRectangle(SolidPen, x, y, x2, y2);
// LINE, ok
x := random(Form1.width); y := random(Form1.height); x2 := random(Form1.width); y2 := random(Form1.height);
graphics.DrawLine(SolidPen, x, y, x2, y2);
procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.Height := Screen.Height;
Form1.Width := Screen.Width;
end;
您没有正确传递多边形的点。
查看两个重载的DrawPolygon()
声明:
function TGPGraphics.DrawPolygon(pen: TGPPen; points: PGPPointF; count: Integer): TStatus;
function TGPGraphics.DrawPolygon(pen: TGPPen; points: PGPPoint; count: Integer): TStatus;
您可以看到这些点是作为PGPPointF
或PGPPoint
传递的。这些类型的定义可以在Winapi.GDIPAPI
中找到,在那里我们看到坐标要么single
,要么integer
由于您使用的是整数坐标,请查看 Winapi.GDIPAPI 中PGPPoint
的定义
type
PGPPoint = ^TGPPoint;
TGPPoint = record
X : Integer;
Y : Integer;
end;
TPointDynArray = array of TGPPoint;
function MakePoint(X, Y: Integer): TGPPoint; overload;
{$EXTERNALSYM MakePoint}
所以,声明一个变量
ArrOfPoint: TPointDynArray;
并用您的积分填充它:
SetLength(ArrOfPoint, 3);
ArrOfPoint[0] := MakePoint(x, y);
ArrOfPoint[1] := MakePoint(x2, y2);
ArrOfPoint[2] := MakePoint(x3, y3);
最后替换您的呼叫,例如DrawPolygon()
graphics.DrawPolygon(SolidPen, PGPPoint(@ArrOfPoint[0]), 3);
这意味着您将第一个点的地址作为PGPPoint
类型传递。
因此,多边形的更正工作代码如下(目前(:
uses
... GDIPAPI, GDIPOBJ, GDIPUTIL;
procedure TForm1.Button1Click(Sender: TObject);
var
graphics: TGPGraphics;
SolidPen: TGPPen;
SolidBrush : TGPBrush;
ArrOfPoint: TPointDynArray;
x, y, x2, y2, x3, y3 : integer;
begin
graphics := TGPGraphics.Create(Canvas.Handle);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
SolidPen := TGPPen.Create(MakeColor(255, random(255), random(255), random(255)), random(4)+1);
SolidBrush := TGPSolidBrush.Create(MakeColor(255, random(255), random(255), random(255)));
x := random(Form1.Width); y := random(Form1.Height);
x2 := random(Form1.Width); y2 := random(Form1.Height);
x3 := random(Form1.Width); y3 := random(Form1.Height);
SetLength(ArrOfPoint, 3);
ArrOfPoint[0] := MakePoint(x, y);
ArrOfPoint[1] := MakePoint(x2, y2);
ArrOfPoint[2] := MakePoint(x3, y3);
graphics.FillPolygon(SolidBrush,PGPPoint(@ArrOfPoint[0]), 3);
graphics.DrawPolygon(SolidPen, PGPPoint(@ArrOfPoint[0]), 3);
end;