Inherited TShape.Paint does not work after overriding Shape property
I'm to code a TExpandedShape class inherited from TShape. TExpandedShape must act like TShape and be able to draw extra shapes: Polygon and Star. Here is my code
unit ExpandedShape;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, Windows;
type
TExpandedShapeType = (
stRectangle, stSquare, stRoundRect, stRoundSquare, stEllipse, stCircle,
stPolygon,
stStar
);
TExpandedShape = class(TShape)
private
FShape: TExpandedShapeType;
FEdgeCount: integer;
procedure SetShape(const Value: TExpandedShapeType);
procedure SetEdgeCount(const Value: integer);
public
procedure Paint; override;
published
property Shape : TExpandedShapeType read FShape write SetShape;// default stPolygon;
property EdgeCount : integer read FEdgeCount write SetEdgeCount default 5;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Course', [TExpandedShape]);
end;
// TExpandedShape
procedure TExpandedShape.Paint;
begin
case Shape of
stStar : begin {Draw Star}
end;
stPolygon : begin {Draw Polygon}
end;
else begin
{It is supposed to draw Circle, Rectangle etc, but it does not}
inherited;
end;
end;
end;
procedure TExpandedShape.SetEdgeCount(const Value: integer);
begin
FEdgeCount := Value;
Repaint;
end;
procedure TExpandedShape.SetShape(const Value: TExpandedShapeType);
begin
FShape := Value;
Repaint;
end;
end.
So, what is wrong?
IMO TShape.Paint checks private value like FShape in case section and then decides what to draw. When inherited Paint method is called in my code it checks FShape value sees default 0 value [stRectangle] in there and draws it.PS: I did solve it with blackmagic way using Shape1 property instead of Shape one and if Shape1 value is not stPolygon or stStar i do like this: begin Shape := TShapeType(Shape1); inherited end; But this option is not really an opti开发者_运维技巧on. I need a good short nice-looking one.
add this inherited line before your inherited....
inherited Shape := TShapeType(Shape); // ** add this line **
inherited;
Here in my web you can find an article about PlugIns in Delphi.
The sample code of the article implement a descendant class of TShape. All the code is included. You can download and see the code of class.
Another descendant classes implement figures lile Stars, Arrows,...
Here you can see some figures descendant of TShape implemented.
Neftalí
P.D: Excuse-me for mistakes with english.
精彩评论