Extending TShape control lazarus / 31-LCL ---------------------------------------------------------------------- simsee · 2023-04-30 17:53 I premise that my knowledge of LCL is not deep. For this I ask for help from those of you more experienced. I want to extend TShape so that it can draw other shapes currently supported. For example a simple line. My code is as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    procedure FormShow(Sender: TObject);  private   public   end;   TShapeType = (stRectangle, stSquare, stRoundRect, stRoundSquare,    stEllipse, stCircle, stSquaredDiamond, stDiamond,    stTriangle, stTriangleLeft, stTriangleRight, stTriangleDown,    stStar, stStarDown, stLine); //stLine new shape type   { TMyShape }   TExtShape=class(TShape)    private      fShapeType : TShapeType;    public      property ShapeType : TShapeType read fShapeType write fShapeType;      procedure Paint; override;  end; var  Form1: TForm1; implementation {$R *.lfm} { TMyShape } procedure TExtShape.Paint;begin  if fShapeType=stLine then    Canvas.Line(Left,Top,Left+Width,Top+Height)  else    inherited Paint;end; { TForm1 } procedure TForm1.FormShow(Sender: TObject);begin  with TExtShape.Create(Self) do    begin      Parent:=Self;      ShapeType:=stLine;      Pen.Color:=clRed; //It does not work      SetBounds(10,10,500,500);    end;end; end. While it appears to work, it is actually incorrect. For example, I can't set the color of the new shape of type stLine, because in the overridden method Paint I don't call inherithed in this case. If I did, the default shape would in fact be drawn. How can I overcome the problem? Thank you. ---------------------------------------------------------------------- wp · 2023-04-30 18:13 In the new shape's Paint procedure you do not apply the Pen to the Canvas: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TExtShape.Paint;begin  if fShapeType=stLine then  begin    Canvas.Pen.Assign(Pen);    Canvas.Line(Left,Top,Left+Width,Top+Height);  end  else    inherited Paint;end;  { TForm1 } procedure TForm1.FormShow(Sender: TObject);begin  with TExtShape.Create(Self) do  begin    Parent:=Self;    ShapeType:=stLine;    Pen.Color:=clRed;    Pen.Width := 3;    Pen.Style := psDash;    SetBounds(10,10,500,500);  end;end; ---------------------------------------------------------------------- simsee · 2023-04-30 18:19 Thanks Wp (I was hoping it was you to answer, thanks!). I thought about this solution too. But examining TShape.Paint procedure I see that there are a series of preliminary operations and checks, before starting the drawing of the figures. I guess I have to repeat them all in the overridden Paint method. Right? ---------------------------------------------------------------------- wp · 2023-04-30 21:19 I looked at the TShape.Paint method, it is not very friendly for overwriting. You certainly must repeat copying the Pen and Brush to the Canvas; you may also want the half-pen widths PenInc and PenDec to avoid clipping of the half line width when the lines reach the control bounds. And, depending on how flexible you want your component to be, you should also fire the OnPaint event at the end. Be careful with the TShapeType. It already exists in the inherited component as published property Shape. So, when you add a new ShapeType property you still have the inherited Shape - which one will win when the user sets Shape to stRectangle and ShapeType to stCircle? Basically you should remove the property Shape, so that there is only your ShapeType. But Shape is published, and it is not possible to reduce its visiblity. It is possible to hide it in the object inspector, but you still can access it at runtime... One solution could be to keep both properties: the original Shape: TShapeType = (stRectangle..stStar) and your OverrideShape: TShapeTypeExt = (stNormal, stLine, etc). The option stNormal would mean that one of the original Shape types should be used for drawing. Not very nice... The other solution is not very satisfying either: Since TShape is rather simple you could rewrite it from scratch, copying everything that you need and replace the TShape by your extended shape types. But doing this you will have the same mess. Writing a component descending from TShape is not a very instructive exercise... ---------------------------------------------------------------------- simsee · 2023-04-30 22:02 Thanks wp. I don't have much experience writing custom components. I have studied your excellent chapter on this topic in the Lazaurs Handbook, so I'm happy to receive your response. In fact I'm realizing that writing a component that has TShape as an ancestor is not simple. However, this is a need I have in my application. Your suggestions are helpful for this purpose. ---------------------------------------------------------------------- wp · 2023-04-30 22:19 How often do you need this component? Only in this project? In such a case I do not see a justification to take the effort of creating a dedicated component if there are other ways to achieve what you need. For example, you could use the OnPaint event of the TShape to override painting with "your" shapes. Or you don't need any component at all, you could paint directly on the canvas of the form, panel, or whatever, in their OnPaint event. Here is an example how to draw your "stLine" in a standard TShape by means of its OnPaint event: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Shape1Paint(Sender: TObject);begin  Shape1.Canvas.Pen.Color := clRed;  Shape1.Canvas.Pen.Style := psSolid;  Shape1.Canvas.Line(10, 10, Shape1.Width-10, Shape1.Height-10);end; procedure TForm1.FormCreate(Sender: TObject);begin  // We do not want to see the inherited shape types.  Shape1.Pen.Style := psClear;  Shape1.Brush.Style := bsClear;end; And here is an example how to draw on the form directly: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.FormPaint(Sender: TObject);begin  Canvas.Pen.Color := clRed;  Canvas.Line(10, 10, 200, 200);end; ---------------------------------------------------------------------- simsee · 2023-04-30 22:27 Thanks for the interesting ideas. In the meantime I did the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    procedure FormShow(Sender: TObject);  private   public   end;   TExtShapeType = (stRectangle, stSquare, stRoundRect, stRoundSquare,    stEllipse, stCircle, stSquaredDiamond, stDiamond,    stTriangle, stTriangleLeft, stTriangleRight, stTriangleDown,    stStar, stStarDown, stLine);   { TMyShape }   TExtShape=class(TShape)    private      fExtShape : TExtShapeType;    public      property ExtShape : TExtShapeType read fExtShape write fExtShape;      procedure Paint; override;  end; var  Form1: TForm1; implementation {$R *.lfm} { TMyShape } procedure TExtShape.Paint;begin  if fExtShape=stLine then    begin      Canvas.Pen.Assign(Pen);      Canvas.Brush.Assign(Brush);      Canvas.Line(Left,Top,Left+Width,Top+Height);    end  else    begin    Shape:=TShapeType(fExtShape);      inherited Paint;    end;end; { TForm1 } procedure TForm1.FormShow(Sender: TObject);begin  with TExtShape.Create(Self) do    begin      Parent:=Self;      ExtShape:=stLine;      Pen.Color:=clRed;      SetBounds(10,10,500,500);    end;   with TExtShape.Create(Self) do    begin      Parent:=Self;      ExtShape:=stCircle;      Pen.Color:=clRed;      SetBounds(10,10,500,500);    end;end; end. It's not elegant, I admit it...