【发布时间】:2010-03-30 13:00:23
【问题描述】:
我要编写一个继承自 TShape 的 TExpandedShape 类。 TExpandedShape 必须像 TShape 一样,并且能够绘制额外的形状:多边形和星形。 这是我的代码
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 在 case 部分检查像 FShape 这样的私有值,然后决定绘制什么。当在我的代码中调用继承的 Paint 方法时,它会检查 FShape 值是否在其中看到默认的 0 值 [stRectangle] 并绘制它。
PS:我确实通过使用 Shape1 属性而不是 Shape one 的 blackmagic 方式解决了它,如果 Shape1 值不是 stPolygon 或 stStar 我这样做:begin Shape := TShapeType(Shape1);继承结束;但是这个选项并不是一个真正的选项。我需要一个好看的短款。
【问题讨论】:
标签: delphi overriding vcl delphi-2007