如何防止以派生形式修改设计时组件

How to prevent modifying a design time component in derived form

我定义了一个基本 VCL 表单 class,上面有一个(非可视化)设计时组件,其中包含一组样式。

我想阻止开发人员(和我自己)更改从我的基本表单派生的表单中的那些样式。用 C# 术语来说,我希望组件以基本 VCL 形式密封。

我怎样才能做到这一点?

附带说明:我从来没有理解 Delphi 中表单上设计时组件的声明。有点 public 但没有放在 public 部分 class声明。谁能解释一下这是什么原因?

嗯,简答你不能。如果您真的需要它,我建议您在 运行 时间内创建组件并将其设为私有。如果您正在使用 GExpert (click here),您可以使用 "Components to Code" 命令来获取所需的代码。如果您需要更多详细信息,请分享您的 DFM 文件,我将 post 相应的代码放在这里。

至于你的旁注,这些组件被声明为已发布。请参阅 Visibility of Class Members 引用

If a member's declaration appears without its own visibility specifier, the member has the same visibility as the one that precedes it. Members at the beginning of a class declaration that do not have a specified visibility are by default published, provided the class is compiled in the {$M+} state or is derived from a class compiled in the {$M+} state; otherwise, such members are public

NB TFormTPersistent 的后代,它是用 {$M+} 指令编译的

PS TComponent.SetName(实际上是TComponent.SetReference)需要发布的声明,当您正在修改组件 Name 属性.