2010-03-30 56 views
0

我要编写从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);继承结束;但是这个选项并不是真正的选择。我需要一个好看的好看的。

回答

1

添加此行继承继承您之前....

inherited Shape := TShapeType(Shape); // ** add this line ** 
    inherited; 
+0

谢谢!很棒 – DarkWalker 2010-03-31 13:47:19

1

Here in my web你可以找到关于德尔福插件的文章。
本文的示例代码实现了TShape的后代类。所有的代码都包含在内。你可以下载并查看课程代码。

另一个子类实现数字理了理星,箭,...

alt text

在这里你可以看到一些数字T形的后代实现。

alt text


Neftalí

Pd积:对不起,我要与英语的错误。