想做一个简单的复合组件,像下图所示:控件继承自TCustomPanel,上面放两个Panel,上部的Panel靠上对齐,下部的Panel充满客户区。要控制在设计期向控件添加子控件时不能添加到上部的Panel2,只能添加到下部的Panel3中。就像向PageControl添加控件时只能添加到其中的TabSheet一样。
看了PageControl的源码,没有找到那里实现的。
欢迎大家讨论,可用分非常多,回头可以另开贴添加。呵呵

解决方案 »

  1.   


    unit Custompanel1;interfaceuses
      SysUtils, Classes, Controls, ExtCtrls;type
      TCustompanel1 = class(TPanel)
      private
        FP1: TCustomPanel;
        FP2: TCustomPanel;
      protected
        { Protected declarations }
      public
        constructor Create(AOwner: TComponent); override;
        function GetChildParent: TComponent; override;
        procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
      published
        property P1: TCustomPanel read FP1;
        property P2: TCustomPanel read FP2;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Samples', [TCustompanel1]);
    end;{ TCustompanel1 }constructor TCustompanel1.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);
      FP1 := TPanel.Create(Self);
      FP2 := TPanel.Create(Self);
      FP1.Parent := Self;
      FP2.Parent := Self;
      FP1.Align := alTop;
      FP2.Align := alClient;
      Self.ControlStyle := Self.ControlStyle - [csAcceptsControls];
      FP1.ControlStyle := FP1.ControlStyle - [csAcceptsControls];
    end;function TCustompanel1.GetChildParent: TComponent;
    begin
      Result := FP2;
    end;procedure TCustompanel1.GetChildren(Proc: TGetChildProc; Root: TComponent);
    var
      I: Integer;
      Control: TControl;
    begin
      for I := 0 to FP2.ControlCount - 1 do
      begin
        Control := FP2.Controls[I];
        if Control.Owner = Root then Proc(Control);
      end;
    end;end.
      

  2.   

    厉害,太厉害了。开始看代码的时候有过怀疑GetChildren函数,不过那里并没有GetChildParent函数,所以没有深究。不过这里还是有一个小问题,因为控件本身不再接收自控件,所以在delphi IDE的Object TreeView看到它的子控件的上级不再是他本身,而变成了窗体。虽然这并不影响使用,但总不是那么完美。这有解决办法吗。
      

  3.   

    jjwwang本来就是横跨C++Builder和Delphi的大牛. 赞一个先.
      

  4.   

    不过这里还是有一个小问题,因为控件本身不再接收自控件,所以在delphi IDE的Object TreeView看到它的子控件的上级不再是他本身,而变成了窗体。虽然这并不影响使用,但总不是那么完美。这有解决办法吗。改成这样看是不是要的效果。
      FP1 := TPanel.Create(AOwner);
      FP1.Name := 'MyChildPanel1';
      FP2 := TPanel.Create(AOwner);
      FP2.Name := 'MyChildPanel2';
      

  5.   

    组合控件:
    在Torry中搜Firebird
      

  6.   

    如果子控件的Owner改为父控件的Owner,是可以在Object TreeView中显示完整层次了,但是在设计期也能删除修改了,删除以后程序就出错了。有没有更完美的解决方案,呵呵