Firemonkey:如何定义包含另一个组件的组件? [英] Firemonkey: How to define a component that contain another component?

查看:82
本文介绍了Firemonkey:如何定义包含另一个组件的组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Delphi下,我想创建一个新的firemonkey控件,其中将包含另一个firemonkey控件。这不是真正的问题,因为我可以这样:

Under Delp i want to create a new firemonkey control that will contain another firemonkey control. This is not really a problem because i can do like this :

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited;
  FBtn := Trectangle.create(self);
  FBtn.parent := self;
  FBtn.stored := false;
end;

但是现在我想允许最终用户在中修改FBtn的属性。 >对象检查器也可以!我不知道该怎么做:(

But now i would like to permit end-user to modifie the properties of the FBtn in the Object Inspector also! i don't know how to do :(

如果我删除了FBtn.stored:= False,那么我将在结构浏览器中使用一些名为<的组件。 Components [7]>,并且每次我将文本形式查看并再次以表单形式查看时,结构浏览器中都会出现一个新组件:(

if i remove the FBtn.stored := False then i will have in the structure explorer some component with name like < Components[7] > and every time i will do view the form as text and back to view as form then a new component will appear in the structure explorer :(

推荐答案

虽然从技术上讲雷米的答案是可行的,但我认为这不是合适的方法,这是因为当您将任何 TComponent 后代发布为属性,假定您将在外部 创建此类控件,然后在对象检查器中分配该控件。当然,您不想允许这种情况发生,因为此控件是在内部创建的。您可能还只想允许用户更改某些某些属性,而不是全部更改。

While Remy's answer technically will work, I wouldn't consider it the appropriate approach. This is because when you publish any TComponent descendant as a property, this presumes that you would create such a control externally and then assign it within the object inspector. Surely you don't want to allow this to happen, since this control is created internally. You also presumably only want to allow the user to change certain properties, not all of them.

相反,我建议使用 TPersistent 仅公开您真正想要的控件的那些属性

Instead, I would recommend using a TPersistent which exposes only those properties of the control you actually want to provide access to.

unit MyComponentUnit;

interface

uses
  System.Classes, System.SysUtils,
  Fmx.Controls, FMX.StdCtrls, FMX.Objects;

type
  TMyComponentButton = class(TPersistent)
  private
    FBtn: TRectangle;
    function GetXRadius: Single;
    function GetYRadius: Single;
    procedure SetXRadius(const Value: Single);
    procedure SetYRadius(const Value: Single);
    //And any other property getters/setters you wish to wrap
  public
    constructor Create(ABtn: TRectangle);
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
  published
    property XRadius: Single read GetXRadius write SetXRadius;
    property YRadius: Single read GetYRadius write SetYRadius;
    //And any other properties you wish to wrap
  end;

  TMyComponent = class(TControl)
  private
    FBtn: TRectangle;
    FMyComponentButton: TMyComponentButton;
    procedure SetMyComponentButton(const Value: TMyComponentButton);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property MyComponentButton: TMyComponentButton
      read FMyComponentButton write SetMyComponentButton;
  end;

implementation

{ TMyComponentButton }

constructor TMyComponentButton.Create(ABtn: TRectangle);
begin
  inherited Create;
  FBtn:= ABtn;
end;

destructor TMyComponentButton.Destroy;
begin

  inherited;
end;

procedure TMyComponentButton.Assign(Source: TPersistent);
begin
  if Source is TMyComponentButton then begin
    XRadius:= (Source as TMyComponentButton).XRadius;
    YRadius:= (Source as TMyComponentButton).YRadius;
    //And everything else you need to assign
  end else
    inherited;
end;

function TMyComponentButton.GetXRadius: Single;
begin
  Result:= FBtn.XRadius;
end;

function TMyComponentButton.GetYRadius: Single;
begin
  Result:= FBtn.YRadius;
end;

procedure TMyComponentButton.SetXRadius(const Value: Single);
begin
  FBtn.XRadius:= Value;
end;

procedure TMyComponentButton.SetYRadius(const Value: Single);
begin
  FBtn.YRadius:= Value;
end;

{ TMyComponent }

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited;
  FBtn:= TRectangle.Create(Self);
  FBtn.Parent:= Self;
  FBtn.Stored:= False;
  FMyComponentButton:= TMyComponentButton.Create(FBtn);
end;

destructor TMyComponent.Destroy;
begin
  FreeAndNil(FMyComponentButton);
  FreeAndNil(FBtn);
  inherited;
end;

procedure TMyComponent.SetMyComponentButton(const Value: TMyComponentButton);
begin
  FMyComponentButton.Assign(Value);
end;

end.

这篇关于Firemonkey:如何定义包含另一个组件的组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆