如何在设计时接受其他控件的子面板创建自定义控件? [英] How do I create a custom control with a sub-panel that accepts other controls at designtime?

查看:142
本文介绍了如何在设计时接受其他控件的子面板创建自定义控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个自定义控件,它有几个子面板。我想让这些子面板在设计时接受任何额外的控件。



不幸的是,在设计时丢弃的任何控件都将结束于我的自定义控件,而不是在面板。这显示特别是如果我试图放下一个标签:标签的蓝点被显示,但标题不是,如果我取消选择该标签,它根本不再可见。



简化代码(仅一个子面板):

 键入
TMyContainer = class(TPanel)
p_SubPanel:TPanel;
public
构造函数Create(_Owner:TComponent);覆盖
结束

构造函数TMyContainer.Create(_Owner:TComponent);
开始
继承;
p_SubPanel:= TPanel.Create(Self);
p_SubPanel.Parent:=自我;
p_SubPanel.Align:= alClient;
结束

我在这里做错了什么?



(为了防万一重要:我使用的是Delphi 2007。)





我有现在解决了不同的方式。组件不再包含面板,而是指外部面板。这使得它实际上更加灵活,但是不利之处在于它不再像使用一样直观。



我仍然想知道如何完成我原来描述的内容。在这里没有一个开源组件,所以我可以研究源代码?

解决方案

这是一个很好的题。您可以通过在控件ControlStyle属性中添加csAcceptControl来允许您的自定义TWinControl在设计时丢弃其他控件。

 构造函数TMyContainer.Create(AOwner:TComponent); 
开始
继承;
ControlStyle:= ControlStyle + [csAcceptControls];
结束

但是,为了努力工作,我没有成功,可以将控件放到自定义控件中的子面板。将csAcceptControl添加到子面板的ControlStyle是不够的。我已经得到的美味是一个黑客,以说服它的设计如下:

  type 
TGiveMeProtected_Component = class(TComponent);

程序TMyContainer.Create(AOwner:TComponent);
begin
FSubPanel:= TPanel.Create(Self);
TGiveMeProtected_Component(FSubPanel).SetDesigning(True,True);
结束

使用该代码,您现在可以将控件放在子面板上,但这意味着您也可以选择子面板,改变它的属性,甚至删除你绝对不想要的。对不起,我无法想出答案,我还是很想知道你的工作。 :)


I have written a custom control, that has several sub panels. I want these sub panels to accept any additional controls dropped on them at design time.

Unfortunately any control that gets dropped at design time ends up on my custom control, not on the panels. This shows in particular if I try to drop a label: The label's blue dots are shown, but it's caption isn't and if I deselect the label, it is no longer visible at all.

simplified code (only one sub panel):

type
  TMyContainer = class(TPanel)
    p_SubPanel: TPanel;
  public
    constructor Create(_Owner: TComponent); override;
  end;

  constructor TMyContainer.Create(_Owner: TComponent);
  begin
    inherited;
    p_SubPanel := TPanel.Create(Self);
    p_SubPanel.Parent := Self;
    p_SubPanel.Align := alClient;
  end;

What am I doing wrong here?

(Just in case it matters: I am using Delphi 2007.)

[edit]

I have now solved it differently. The component no longer contains panels but refers to external panels. This makes it actually much more flexible, but on the downside it is no longer as intuitive to use.

I would still like to know how to accomplish what I described originally. Isn't there an open source component somewhere that does this, so I can study the source code?

解决方案

This is a good question. You can allow your custom TWinControl to have other controls dropped on it at design-time by adding csAcceptControls to your controls ControlStyle property.

constructor TMyContainer.Create(AOwner: TComponent);
begin
  inherited;
  ControlStyle := ControlStyle + [csAcceptControls];
end;

But in trying to work this out, I've had little success with being able to drop controls onto a sub panel within a custom control. Adding csAcceptControls to a sub panel's ControlStyle isn't enough. The cloest I've gotten is a hack to convince the sub panel it's being designed like so:

type
  TGiveMeProtected_Component = class(TComponent);

procedure TMyContainer.Create(AOwner: TComponent);
begin
  FSubPanel := TPanel.Create(Self);
  TGiveMeProtected_Component(FSubPanel).SetDesigning(True, True);
end;

Using that code, you can now drop controls onto the sub panel, but it means you can also select the sub panel, change it's properties and even delete it which you definately don't want. Sorry I couldn't come up with the answer, I'd still love to know if you work it out. :)

这篇关于如何在设计时接受其他控件的子面板创建自定义控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆