Delphi - 视觉表单继承 - 组件基类检测

发布于 2024-10-12 03:01:35 字数 583 浏览 3 评论 0原文

我正在使用 VFI(视觉表单继承),我需要检查实例化表单的组件是否属于表单类或表单超类。

有什么想法吗?

  unit1

  TFormStatus = class(TForm)
    cpPanel: TPanel;
    lblStatus: TLabel;
  end;

  unit 2

  TFormCodigo = class(TFormStatus)
    lblCodigo: TLabel;
  end;

  frmCodigo: TFormCodigo:

在 frmCodigo 的任何实例中,我想检测 lblCodigo 是 TFormCodigo 的本地组件,并且 cpPanel / lblStatus 是继承的组件;

  for i:=0 to Self.ComponentCount-1 do begin        
      if "InheritedComponent" (Self.Components[i]) then ...
  end;

使用 RTTI 作为对象属性可以实现类似的操作,但我不知道对于组件是否可以。

谢谢。

I'm using VFI (Visual Form Inheritance) and I need to check if a component of an instantiated form belongs to the formclass or to the form superclass.

any ideas ?

  unit1

  TFormStatus = class(TForm)
    cpPanel: TPanel;
    lblStatus: TLabel;
  end;

  unit 2

  TFormCodigo = class(TFormStatus)
    lblCodigo: TLabel;
  end;

  frmCodigo: TFormCodigo:

In any instances of frmCodigo I want to detect that lblCodigo is local to TFormCodigo and cpPanel / lblStatus are inherited components;

  for i:=0 to Self.ComponentCount-1 do begin        
      if "InheritedComponent" (Self.Components[i]) then ...
  end;

Something like this is possible using RTTI for object properties, but I dont know if it is possible for components.

Thanks.

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

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

发布评论

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

评论(3

玉环 2024-10-19 03:01:35

如果我理解正确的话,您需要 TRttiMember.Parent。例如,请参阅 Rob Love 撰写的这篇文章。我认为您需要 Delphi 2010 或更高版本。

事实上,这只是 优秀文章系列 - 这些文章还将告诉您如何获取字段、属性等,而不必知道它们的名称。

If I understand you correctly, you need TRttiMember.Parent. For example see this article by Rob Love. You'll need Delphi 2010 or later I think.

In fact this is just part of an excellent series of articles - these articles will also tell you how to get hold of the fields, properties etc. without having to know their names.

久夏青 2024-10-19 03:01:35

也许像这样“愚蠢”的东西

function TFormStatus.IsStatusComponent(AComponent: TComponent): Boolean;
begin
  Result := (AComponent = cpPanel) or (AComponent = lblStatus);
end;

已经满足了您的需求?

Maybe something "stupid" like

function TFormStatus.IsStatusComponent(AComponent: TComponent): Boolean;
begin
  Result := (AComponent = cpPanel) or (AComponent = lblStatus);
end;

already fulfils your needs?

源来凯始玺欢你 2024-10-19 03:01:35

在 TFormCordigo 中,您可以重写每次读取特定表单的资源时调用的 ReadState 方法。继承后,称为 ComponentCount 包含截至层次结构的当前成员创建的组件数量,因此您毕竟拥有可以保存在其他地方的组件边框列表。

下面的代码说明了这种方法

procedure TInhTestForm.Button3Click(Sender: TObject);
var
  i: integer;
begin
  inherited;

  Memo1.Lines.Clear;
  for i:=0 to ComponentCount-1 do
  begin
    Memo1.Lines.Add(format('%s inroduced in %s', [Components[i].Name, ComponentParent(i).ClassName]));
  end;
end;

function TInhTestForm.ComponentParent(Index: integer): TClass;
var
  i, j: integer;
begin
  Result:=Nil;
  for i:=Low(fComponentBorders) to High(fComponentBorders) do
  begin
    if Index <= fComponentBorders[i] - 1 then
    begin
      j:=i;
      Result:=Self.ClassType;
      while j < High(fComponentBorders) do
      begin
        Result:=Result.ClassParent;
        Inc(j);
      end;
      break;
    end;
  end;
end;

procedure TInhTestForm.ReadState(Reader: TReader);
begin
  inherited;
  SetLength(fComponentBorders, Length(fComponentBorders) + 1);
  fComponentBorders[High(fComponentBorders)]:=ComponentCount;
end;

In your TFormCordigo you can override ReadState method that is called every time a resource is read for a particular form. After inherited called ComponentCount contains the number of components created up to the current member of hierarchy, so after all you have list of borders for components that you can save elsewhere.

The code below illustrates this approach

procedure TInhTestForm.Button3Click(Sender: TObject);
var
  i: integer;
begin
  inherited;

  Memo1.Lines.Clear;
  for i:=0 to ComponentCount-1 do
  begin
    Memo1.Lines.Add(format('%s inroduced in %s', [Components[i].Name, ComponentParent(i).ClassName]));
  end;
end;

function TInhTestForm.ComponentParent(Index: integer): TClass;
var
  i, j: integer;
begin
  Result:=Nil;
  for i:=Low(fComponentBorders) to High(fComponentBorders) do
  begin
    if Index <= fComponentBorders[i] - 1 then
    begin
      j:=i;
      Result:=Self.ClassType;
      while j < High(fComponentBorders) do
      begin
        Result:=Result.ClassParent;
        Inc(j);
      end;
      break;
    end;
  end;
end;

procedure TInhTestForm.ReadState(Reader: TReader);
begin
  inherited;
  SetLength(fComponentBorders, Length(fComponentBorders) + 1);
  fComponentBorders[High(fComponentBorders)]:=ComponentCount;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文