2010-03-30 57 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積:對不起,我要與英語的錯誤。