【问题标题】:Adding a TGlowEffect to a derived firemonkey TImage component将 TGlowEffect 添加到派生的 firemonkey TImage 组件
【发布时间】:2014-12-14 08:14:43
【问题描述】:

我有一个从具有 TGlowEffect 的 TImage 派生的组件。我已经安装了它,它在表单上可见。我可以更改发光效果参数,但是当我运行程序时,程序会导致 Rad Studio 崩溃?

这是我的代码。

type
   TGlowImage = class( TImage )  
   private  
       FGlowEffect: TGlowEffect;  

       procedure SetGlowEffect( const Value : TGlowEffect );  

    protected

    public
      constructor Create( AOwner : TComponent); override;
      destructor Destroy(); override;

    published
      property GlowEffect : TGlowEffect read FGlowEffect write SetGlowEffect;
    end;

procedure Register;  

implementation  

procedure Register;  
  begin  
    RegisterComponents( 'SomeCompany', [TGlowImage] );  
  end;  

{ TGlowImage }  

constructor TGlowImage.Create( AOwner : TComponent );  
  begin  
    inherited;  
      FGlowEffect := TGlowEffect.Create( Self )  
  end;  

destructor TGlowImage.Destroy();  
  begin  
    if( Assigned( FGlowEffect ) ) then  
      FreeAndNil( FGlowEffect );  
    inherited;  
  end;  

procedure TGlowImage.SetGlowEffect( const Value : TGlowEffect );  
  begin  
    FGlowEffect.Assign( Value );  
  end;  

我不知道我错过了什么,但一定很糟糕。

任何帮助将不胜感激。

【问题讨论】:

  • “程序崩溃 RAD Studio”不是一个有用的问题描述。您收到错误消息吗?如果是这样,它说明了什么?在测试之前不要安装组件。为此,您可以在测试应用程序的代码中创建它,并将参数设置为不同的值;如果它不起作用,您可以轻松地使用调试器来确定原因。彻底测试后,然后安装它。
  • 不是崩溃的原因,但在你的析构函数中没有必要在 FreeAndNil 之前调用 Assigned。可以使用 nil 引用安全地调用 FreeAndNil。

标签: delphi


【解决方案1】:

问题是,您不能将TGlowEffect 分配给TGlowEffect。如果你运行代码,你会得到一个 EConvertError 异常(TGlowEffect 不能分配给 TGlowEffect)。 我认为您可以只使用标准 TImage 并将 TGlowEffect 添加为 ChildComponent 或者您应该将 TGlowEffect 实现为只读属性,以便您可以访问效果的属性。

TGlowImage=class(TImage)
private
  FGlowEffect: TGlowEffect;
public
  constructor Create(AOwner: TComponent); override;
  destructor Destroy(); override;
published
  property GlowEffect: TGlowEffect read FGlowEffect;
end;

{ TGlowImage }

constructor TGlowImage.Create(AOwner: TComponent);
begin
  inherited;
  FGlowEffect:=TGlowEffect.Create(Self);
  Self.AddObject(FGlowEffect);
end;

destructor TGlowImage.Destroy();
begin
  FreeAndNil(FGlowEffect);
  inherited;
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-15
    • 2017-04-06
    • 2018-02-26
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    相关资源
    最近更新 更多