【问题标题】:Graphics.Polygon code not working - what am I missing?Graphics.Polygon 代码不起作用 - 我错过了什么?
【发布时间】:2020-08-26 12:41:40
【问题描述】:

我的小代码创建了具有不同形状的全屏尺寸表格画布的现代艺术。 我可以制作椭圆、矩形和线条,但不能制作多边形。谁能帮我? (版本: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+


【解决方案1】:

因此,多边形的更正工作代码如下(目前):

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;

【讨论】:

    【解决方案2】:

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

    查看两个重载的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 类型传递。

    【讨论】:

    • 有效!非常感谢汤姆。起初我很困惑,因为使用传统图形制作多边形非常容易。
    • 不客气。现在你已经开始工作了,考虑一下 David Heffernan 的评论,并将代码移动到你的表单或任何你想在上面绘制它的 OnPaint 事件。
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2020-09-14
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    相关资源
    最近更新 更多