我要編寫從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