我要编写从TShape继承的TExpandedShape类。 TExpandedShape必须像TShape一样行动并能够绘制多余的形状:多边形和星形。 这里是我的代码重写Shape属性后,继承的TShape.Paint不起作用
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
{它应该画圆形,矩形等,但它并不}
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.
那么,什么是错的?
IMO TShape.Paint在案例部分检查像FShape这样的私有值,然后决定要绘制什么。当我的代码调用继承的Paint方法时,它会检查FShape值,并在其中查看默认的0值[stRectangle]并绘制它。我已经用blackmagic方式使用Shape1属性而不是Shape来解决它,如果Shape1的值不是stPolygon或stStar,我喜欢这样做:begin Shape:= TShapeType(Shape1);继承结束;但是这个选项并不是真正的选择。我需要一个好看的好看的。
谢谢!很棒 – DarkWalker 2010-03-31 13:47:19