【问题标题】:Inherited TShape.Paint does not work after overriding Shape property覆盖 Shape 属性后继承的 TShape.Paint 不起作用
【发布时间】: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


    【解决方案1】:

    在你继承的....之前添加这个继承的行。

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

    【讨论】:

      【解决方案2】:

      Here in my web你可以找到一篇关于 Delphi 插件的文章。
      文章的示例代码实现了TShape的一个后代类。包含所有代码。您可以下载并查看课程代码。

      另一个后代类实现了数字 lile Stars, Arrows,...

      在这里您可以看到一些实现了 TShape 后代的图形。


      内夫塔利

      P.D:请原谅我的英语错误。

      【讨论】:

        猜你喜欢
        • 2012-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多