在运行时创建 Tframe:

发布于 2024-10-07 00:20:15 字数 206 浏览 6 评论 0原文

是否可以创建运行时框架并添加现有面板,例如将面板的父级设置为框架?添加后,复制框架并使用它?

就像:

f:= Tframe. create(..)
...

panel3.parent = f; //where panel3 has many controls.

然后复制 f?有可能吗?如何?或任何其他建议? e

is it posible to create runtime frame and add existing panels like setting the parent of panel to the frame? and when it added, dulplicate the frame and use it?

like:

f:= Tframe. create(..)
...

panel3.parent = f; //where panel3 has many controls.

then duplicate the f? was it posible? how? or any other suggerstion?
e

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

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

发布评论

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

评论(3

我认为你不会通过重复来解决这个问题。你需要的是这样的函数:

function CreateFrameAndHostPanel(Owner: TComponent; Parent: TWinControl; Panel: TPanel): TFrame;
begin
  Result := TFrame.Create(Owner);
  Try
    Result.Parent := Parent;
    Panel.Parent := Result;
  Except
    FreeAndNil(Result);
    raise;  
  End;
end;

I don't think you would solve this by duplicating. What you need is a function like this:

function CreateFrameAndHostPanel(Owner: TComponent; Parent: TWinControl; Panel: TPanel): TFrame;
begin
  Result := TFrame.Create(Owner);
  Try
    Result.Parent := Parent;
    Panel.Parent := Result;
  Except
    FreeAndNil(Result);
    raise;  
  End;
end;
提赋 2024-10-14 00:20:15

您需要记住,所有控件都有一个父控件和一个所有者。所有者可能是nil,但随后您需要通过代码释放这些控件,因此大多数控件由其他某个组件拥有。

因此,如果所有者被摧毁,面板也会被摧毁。如果面板是在设计时创建的,那么它就属于它所在的表单!

破坏那个形态就会破坏面板!

但是,如果您在运行时创建面板并将 Application 设置为所有者而不是表单,则它们可以在多个表单和框架上移动。

但这是一个好的设计模式吗?我不知道你想做什么,但这可能是个坏主意!

一般来说,在设计时就用面板设计整个框架会更实用。然后添加一些代码,允许通过从另一个面板或控件复制数据来创建框架。这将是一个更好的设计模式......

You need to remember that all controls have a parent and an owner. Owners could be nil but then you need to free those controls through code, so most controls are owned by some other component.

Thus, if the owner gets destroyed, the panel would be destroyed too. And if the panel was created in design-time then it's owned by the form that it's on!

Destroying that form would destroy the panel!

But if you create the panels in runtime and set Application as owner instead of a form, they could be moved over multiple forms and frames.

But is it a good design pattern? I don't know what you're trying to do but it's likely a bad idea!

In general, it would be more practical to design the whole frame with panels in design-time. Then add some code that would allow the frame to be created by copying data from another panel or control. That would be a better design pattern...

聚集的泪 2024-10-14 00:20:15

您必须使用与创建第一个框架 (FRAME1) 相同的代码来创建新框架 (FRAME2);之后,您必须在 FRAME2 上创建 FRAME1 内包含(在运行时创建)的所有组件。

为此,请使用:

for i := 0 to (FRAME1.ComponentCount - 1) do 
  ...
  cmp := TComponent(FRAME1.Component[i]);
  ... create cmp  on Frame2

您可以尝试第二种选择;使用 TMemoryStream (SaveComponent) 保存 FRAME1,然后创建新的 Frame 并检索 Stream 上保存的信息(我没有测试此选项)。

问候。

You must create the new frame (FRAME2) with the same code that you have used to create the first (FRAME1); And later, you must create all the component included (created on runtime) inside FRAME1 on FRAME2.

For to this, use:

for i := 0 to (FRAME1.ComponentCount - 1) do 
  ...
  cmp := TComponent(FRAME1.Component[i]);
  ... create cmp  on Frame2

You can try a second alternative; Save the FRAME1 using a TMemoryStream (SaveComponent) and later create the new Frame and retrieve the saved information on Stream (I don't have test this option).

Regards.

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