是否可以? TCollection 后代实现任意内容的 TPanel 容器的存储

发布于 2024-09-25 09:55:42 字数 2380 浏览 9 评论 0原文

我是 Delphi 组件开发的新手,因此想知道是否可以实现我的任务。

我需要创建一个基于 TScrollBox 的可视组件(用户控件),它将代表一堆 TPanel,所有面板将在 TScrollBox 内对齐为“顶部”,并且可以具有不同的高度。它必须充当 TCollection(添加、删除、重新排序),并且必须允许用户在设计时将其他控件添加到这些面板中。

我已经为组件创建了这些类:

type
  TPanelsGrid = class;

  TPanelsGridItem = class(TCollectionItem)
  private
    FPanel: TPanel;
    procedure SetPanel(Value: TPanel);
    function GetGrid: TPanelsGrid;
  protected
    function GetDisplayName: string; override;
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
  published
    // This is my TPanel object that should be used at designtime
    // I thought "stored True" will serialize it automatically but I was wrong
    property Panel: TPanel read FPanel write SetPanel stored True; 
  end;

  TPanelsGridItems = class(TCollection)
  private
    FPanelsGrid: TPanelsGrid;
  protected
    function GetItem(Index: Integer): TPanelsGridItem;
    procedure SetItem(Index: Integer; Value: TPanelsGridItem);

    function GetOwner: TPersistent; override;
    procedure Update(Item: TCollectionItem); override;
  public
    property EditorsGrid: TPanelsGrid read FPanelsGrid;
    property Items[Index: Integer]: TPanelsGridItem
      read GetItem write SetItem; default;

    constructor Create(PanelsGrid: TPanelsGrid);
    function Add: TPanelsGridItem;
    procedure Delete(Index: Integer);
  end;

  TPanelsGrid = class(TScrollBox)
  private
    FItems: TPanelsGridItems;
    procedure SetItems(Value: TPanelsGridItems);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Items: TPanelsGridItems read FItems write SetItems;
  end;

该组件在设计时工作正常,我可以在堆栈中添加删除面板,当我在任何面板上放置某些控件(例如 TCheckbox)时,它显示为“由该面板拥有” :例如,我无法将此复选框拖出面板。

但此复选框不存储在 DFM 文件中,也不显示在“结构”窗口中。

我想 TPanel 的内容一定有一些手动序列化-反序列化,但我不知道该怎么做。在互联网上找不到任何示例。如果这种实施完全可能的话,请给我一些指导。

添加

这就是我的 DFM 文件片段在网格中添加 3 个面板后的样子:

  object PanelsGrid1 : TPanelsGrid 
    Left = 8
    Top = 8
    Width = 536
    Height = 382
    Anchors = [akLeft, akTop, akRight, akBottom]
    TabOrder = 0
    Items = <
      item
      end
      item
      end
      item
      end>
  end

如您所见,所有项目都是空的,但我在项目 #3 中放置了一个复选框和单选按钮。

I'm new to component development in Delphi, therefore want to know, is it possible to implement my task at all.

I need to create a visual component (user control) based on TScrollBox, which will represent a bunch of TPanel, all that panels will be aligned as "Top" inside that TScrollBox and can have different Height. It has to act as TCollection (add, delete. reorder), and must allow users to add other controls into these panels at designtime.

I've created these classes for component:

type
  TPanelsGrid = class;

  TPanelsGridItem = class(TCollectionItem)
  private
    FPanel: TPanel;
    procedure SetPanel(Value: TPanel);
    function GetGrid: TPanelsGrid;
  protected
    function GetDisplayName: string; override;
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
  published
    // This is my TPanel object that should be used at designtime
    // I thought "stored True" will serialize it automatically but I was wrong
    property Panel: TPanel read FPanel write SetPanel stored True; 
  end;

  TPanelsGridItems = class(TCollection)
  private
    FPanelsGrid: TPanelsGrid;
  protected
    function GetItem(Index: Integer): TPanelsGridItem;
    procedure SetItem(Index: Integer; Value: TPanelsGridItem);

    function GetOwner: TPersistent; override;
    procedure Update(Item: TCollectionItem); override;
  public
    property EditorsGrid: TPanelsGrid read FPanelsGrid;
    property Items[Index: Integer]: TPanelsGridItem
      read GetItem write SetItem; default;

    constructor Create(PanelsGrid: TPanelsGrid);
    function Add: TPanelsGridItem;
    procedure Delete(Index: Integer);
  end;

  TPanelsGrid = class(TScrollBox)
  private
    FItems: TPanelsGridItems;
    procedure SetItems(Value: TPanelsGridItems);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Items: TPanelsGridItems read FItems write SetItems;
  end;

This component is working ok at designtime, I can add-delete panels in stack, when I'm dropping some control (e.g. TCheckbox) on any panel, it's displayed as "owned by that panel": e.g. I can't drag this checkbox out of panel.

But this checkbox isn't stored in DFM-file and isn't displayed in "Structure" window.

I guess there must be some manual serialization-deserialization of TPanel's content, but I have no idea how to do that. Can't find any example on Internet. Plase give me some guideline, if such implementation is possible at all.

Addition:

This is how my DFM-file fragment looks like after adding 3 panels into grid:

  object PanelsGrid1 : TPanelsGrid 
    Left = 8
    Top = 8
    Width = 536
    Height = 382
    Anchors = [akLeft, akTop, akRight, akBottom]
    TabOrder = 0
    Items = <
      item
      end
      item
      end
      item
      end>
  end

As you can see, all items are empty but I dropped there a checkbox and radiobutton into item #3.

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

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

发布评论

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

评论(3

爱她像谁 2024-10-02 09:55:42

毕竟,我决定放弃使用 TCollection,因为在 DefineProperties 方法的测试过程中,我遇到了一致的 IDE 崩溃。我认为 TCollection 并不是为此类任务而设计的。

我在控件 ExtCtrls.TCustomCategoryPanelGroup 内的 Delphi 源代码中创建了一个适当的实现。它维护可以在设计时和运行时添加或删除的面板堆栈。我使用 TCustomCategoryPanelGroup 和 TCustomCategoryPanel 的源代码创建了自己的类,它按我想要的方式工作。

After all I decided to give up using TCollection, because during testing of DefineProperties method I has consistent IDE crash. I think TCollection just wasn't designed for such task.

I founded an appropriate implementation inside Delphi sources inside of control ExtCtrls.TCustomCategoryPanelGroup. It maintains the stack of panels which can be added or removed both at design time and runtime. I created my own classes, using the source code of TCustomCategoryPanelGroup and TCustomCategoryPanel and it works as I want.

我的奇迹 2024-10-02 09:55:42

我想你可以看看 TMS Poly List 控件

TMS Advanced Poly List 组件
提供极其通用和
灵活的架构创建
几乎任何可能的项目列表
在用户界面中。这是看到的
通常但不限于新的
Office 2010 应用程序菜单。相反
对于大多数用户界面列表控件,
其中列表由以下项目组成
相同类型或项目的集合
同一类型,TMS Advanced Poly
列表组件可以保存多态
项目。所有物品只需下降
来自基类 TCustomItem 和
可以添加任何继承的项目。经颅磁刺激系统
高级 Poly List 组件来了
有一大组预建列表
项目,但可以自定义项目类别
通过降序添加
TCustomItem 基类或任何
已经提供的课程。有
显示为列表部分的项目类别
项目,具有 HTML 格式的文本项目,
带按钮的文本项目,带按钮的项目
展开/折叠行为,项目
图像等等。物品可以是
添加到多晶型列表中
设计时间,拥有丰富的设计时间
编辑器和运行时通过代码。

I think you can look at the TMS Poly List control

The TMS Advanced Poly List components
offer an extremely versatile and
flexible architecture to create
virtually any possible lists of items
in user interfaces. This is seen
typically but not limited to the new
Office 2010 application menu. Contrary
to most user interface list controls,
where a list consists of items of the
same type or a collection of items of
the same type, the TMS Advanced Poly
List components can hold polymorph
items. All items just need to descend
from the base class TCustomItem and
any inherited items can be added. TMS
Advanced Poly List components come
with a large set of prebuilt list
items but custom item classes can be
added by either descending of the
TCustomItem base class or any of the
classes already provided. There are
item classes to show as list section
item, text item with HTML formatting,
text item with buttons, item with
expand/collaps behaviour, item with
image and many more. Items can be
added in the polymorph lists either at
design time, with a rich design time
editor and at runtime via code.

欲拥i 2024-10-02 09:55:42

确保您的子面板有名称。您可以覆盖 TCollection.Notify,如果 Action 是 cnAdded,请确保面板有一个名称。

Make sure your child panels have names. You can override TCollection.Notify and if Action is cnAdded, make sure the panel has an name.

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