【发布时间】:2011-04-17 19:34:58
【问题描述】:
我是 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 的内容肯定有一些手动序列化-反序列化,但我不知道该怎么做。在 Internet 上找不到任何示例。请给我一些指导,如果这样的实现是可能的。
加法:
这是在网格中添加 3 个面板后我的 DFM 文件片段的样子:
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 中放置了一个复选框和单选按钮。
【问题讨论】:
-
在设计时右键单击您的表单并选择“以文本形式查看”。如果您不介意,请找到与您的组件相关的部分并将其添加到您的帖子中。我自己也不确定这应该如何实现,但让我们先看看存储了什么。
-
从 TComponents 继承的已发布属性不被流系统存储。
-
他自己,完成,请看 dfm。
-
Uwe,所以我必须创建自己的流媒体程序?我应该从哪里开始?
-
没有测试过(否则这将是一个答案):1)了解DefineProperties,2)尝试使面板成为子组件(ComponentState)
标签: delphi components delphi-2010 tcollection tcollectionitem