继承的 TShape.Paint 在重写 Shape 属性后不起作用

发布于 2024-08-27 02:17:49 字数 1889 浏览 2 评论 0原文

我要编写一个继承自 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 检查案例部分中的私有值(如 FShape),然后决定绘制什么。当在我的代码中调用继承的 Paint 方法时,它会检查 FShape 值,在其中看到默认 0 值 [stRectangle] 并绘制它。

PS:我确实使用 Shape1 属性而不是形状一以 blackmagic 方式解决了这个问题,如果 Shape1 值不是 stPolygon 或 stStar 我会这样做: begin Shape := TShapeType(Shape1);继承结束;但这个选项并不是真正的选项。我需要一件漂亮的短款。

I'm to code a TExpandedShape class inherited from TShape. TExpandedShape must act like TShape and be able to draw extra shapes: Polygon and Star.
Here is my code

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 

{It is supposed to draw Circle, Rectangle etc, but it does not}

          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.

So, what is wrong?
IMO TShape.Paint checks private value like FShape in case section and then decides what to draw. When inherited Paint method is called in my code it checks FShape value sees default 0 value [stRectangle] in there and draws it.

PS: I did solve it with blackmagic way using Shape1 property instead of Shape one and if Shape1 value is not stPolygon or stStar i do like this: begin Shape := TShapeType(Shape1); inherited end; But this option is not really an option. I need a good short nice-looking one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-09-03 02:17:49

在继承的...之前添加此继承行

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

add this inherited line before your inherited....

  inherited Shape := TShapeType(Shape);  // ** add this line **  
  inherited;   
埋葬我深情 2024-09-03 02:17:49

在我的网站中,您可以找到一篇有关 Delphi 插件的文章。
本文的示例代码实现了 TShape 的后代类。所有代码都包含在内。您可以下载并查看课程代码。

另一个后代类实现了星星、箭头等图形

alt text

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

alt text


Neftalí

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

Here in my web you can find an article about PlugIns in Delphi.
The sample code of the article implement a descendant class of TShape. All the code is included. You can download and see the code of class.

Another descendant classes implement figures lile Stars, Arrows,...

alt text

Here you can see some figures descendant of TShape implemented.

alt text


Neftalí

P.D: Excuse-me for mistakes with english.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文