Graphics.Polygon代码不起作用-我缺少什么?

问题描述 投票:0回答:2

我的小代码将全屏尺寸的现代艺术创作成具有不同形状的Form画布。我可以使椭圆,矩形和线工作,但不能使polycon工作。谁能帮我? (版本:Delphi社区版)

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;
delphi graphics polygon gdi+
2个回答
0
投票

您没有正确传递多边形的点。

查看两个重载的DrawPolygon()声明:

function TGPGraphics.DrawPolygon(pen: TGPPen; points: PGPPointF; count: Integer): TStatus;
function TGPGraphics.DrawPolygon(pen: TGPPen; points: PGPPoint; count: Integer): TStatus;

您可以看到这些点以PGPPointFPGPPoint的形式传递。这些类型的定义可以在Winapi.GDIPAPI中找到,我们可以看到坐标为singleinteger

由于使用整数坐标,请查看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类型传递。


0
投票

因此,该多边形的更正代码如下:

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;
© www.soinside.com 2019 - 2024. All rights reserved.