I am trying to draw a polygon using GDI. This code works:
type
TPolygon: Array[0..2] of TPoint;
var
ACanvas: TGPGraphics;
MyBrush: TGPLinearGradientBrush;
...
procedure DrawPolygon;
var
Polygon: TPolygon;
begin
Polygon[0].X := 1;
Polygon[0].Y := 5;
Polygon[1].X := 10;
Polygon[1].Y := 15;
Polygon[2].X := 1;
Polygon[2].Y := 5;
ACanvas.FillPolygon(MyBrush, PGPPoint(@Polygon), length(Polygon));
end;
...
This code produces a GDI Value Overflow error:
type
TPolygon: Array of TPoint;
var
ACanvas: TGPGraphics;
MyBrush: TGPLinearGradientBrush;
...
procedure DrawPolygon;
var
Polygon: TPolygon;
begin
SetLength(Polygon, 3);
Polygon[0].X := 1;
Polygon[0].Y := 5;
Polygon[1].X := 10;
Polygon[1].Y := 15;
Polygon[2].X := 1;
Polygon[2].Y := 5;
ACanvas.FillPolygon(MyBrush, PGPPoint(@Polygon), length(Polygon));
end;
...
The only difference is that one point array is dynamic, the other is static. Obviously the underlying memory values are different, but in what way?
Your code is invalid. (First, there is no
FillPolygoninTCanvas, and second, a polygon need at least three vertices. Also, there are some syntax errors, like a : instead of = at the const declaration.) I’d suggest the samplewhich is a nice triangle. Anyhow, while
works for a static array, you have to do
for a dynamic array. The reason is that a static array is stored ‘in-place’ in memory, that is, directly at
@Polygon, just like a number (e.g.,cardinal) is stored, or aShortString, or a record of such simple types. On the contrary, ifPolygonis a dynamic array, then it is really a pointer to the actual, variable-length, data (in much the same way a normal variable-length string works). That is, at@Polygonyou only have a pointer, aNativeUInt. The actual data starts at this new address, which you can get by writing@Polygon[0].