是否可以? TCollection 后代实现任意内容的 TPanel 容器的存储
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
毕竟,我决定放弃使用 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.
我想你可以看看 TMS Poly List 控件
I think you can look at the TMS Poly List control
确保您的子面板有名称。您可以覆盖 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.