LCL/TShape: Add user-defined polygon shape. No allocation of mask in constructor.

This commit is contained in:
wp_xyz 2023-09-11 19:34:18 +02:00
parent b473b8b1ed
commit 3ec349e0eb
2 changed files with 26 additions and 3 deletions

View File

@ -266,7 +266,12 @@ type
TShapeType = (stRectangle, stSquare, stRoundRect, stRoundSquare,
stEllipse, stCircle, stSquaredDiamond, stDiamond,
stTriangle, stTriangleLeft, stTriangleRight, stTriangleDown,
stStar, stStarDown);
stStar, stStarDown, stPolygon);
TShapePoints = array of TPoint;
TShapePointsEvent = procedure (Sender: TObject; var Points: TShapePoints;
var Winding: Boolean) of object;
TShape = class(TGraphicControl)
private
@ -275,9 +280,11 @@ type
FShape: TShapeType;
FBitmapCopy: TBitmap; // For testing if a mouse click is on the actual shape.
FOnShapeClick: TNotifyEvent;
FOnShapePoints: TShapePointsEvent;
procedure SetBrush(Value: TBrush);
procedure SetPen(Value: TPen);
procedure SetShape(Value: TShapeType);
procedure SetOnShapePoints(Value: TShapePointsEvent);
protected
class procedure WSRegisterClass; override;
class function GetControlClassDefaultSize: TSize; override;
@ -329,6 +336,7 @@ type
property OnStartDock;
property OnStartDrag;
property OnShapeClick: TNotifyEvent read FOnShapeClick write FOnShapeClick;
property OnShapePoints: TShapePointsEvent read FOnShapePoints write SetOnShapePoints;
end;

View File

@ -18,8 +18,6 @@ begin
FPen.OnChange := @StyleChanged;
FBrush := TBrush.Create;
FBrush.OnChange := @StyleChanged;
FBitmapCopy := TBitmap.Create;
FBitmapCopy.Monochrome := True;
end;
destructor TShape.Destroy;
@ -79,6 +77,8 @@ var
P: array[0..3] of TPoint;
PStar: array[0..10] of TPoint;
PenInc, PenDec: Integer;
PolygonPts: array of TPoint;
PolygonWinding: Boolean;
RadiusBig, RadiusBig2, RadiusSm, i: Integer;
PCenter: TPoint;
begin
@ -207,6 +207,15 @@ begin
PStar[10] := PStar[0];
ACanvas.Polygon(PStar);
end;
stPolygon:
if Assigned(FOnShapePoints) then
begin
PolygonPts := nil;
PolygonWinding := false;
FOnShapePoints(Self, PolygonPts, PolygonWinding);
if Length(PolygonPts) > 2 then
ACanvas.Polygon(PolygonPts, PolygonWinding);
end;
end;
end;
@ -250,6 +259,12 @@ begin
FBrush.Assign(Value);
end;
procedure TShape.SetOnShapePoints(Value: TShapePointsEvent);
begin
FOnShapePoints := Value;
Invalidate;
end;
procedure TShape.SetPen(Value: TPen);
begin
if Value <> Pen then