How to Make Delphi TPanel designable like TForm & TDataModule

Question/Problem/Abstract:

Make TPanel designable like TForm & TDataModule...

Answer:

unit PanelForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, ComCtrls;

type
  TPanelForm = class(TPanel)
  private
    FOnCreate: TNotifyEvent;
    FOnDestroy: TNotifyEvent;
  protected
    procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
  public
    constructor Create(AOwner: TComponent); override;
    constructor CreateNew(AOwner: TComponent);
    destructor Destroy; override;
  published
    property OnCreate: TNotifyEvent read FOnCreate write FOnCreate;
    property OnDestroy: TNotifyEvent read FOnDestroy write FOnDestroy;
  end;

implementation

uses
  Consts;

procedure TPanelForm.GetChildren(Proc: TGetChildProc; Root: TComponent);
var
  I: Integer;
  OwnedComponent: TComponent;
begin
  inherited GetChildren(Proc, Root);
  if Root = Self then
    for I := 0 to ComponentCount - 1 do
    begin
      OwnedComponent := Components[I];
      if not OwnedComponent.HasParent then Proc(OwnedComponent);
    end;
end;

constructor TPanelForm.Create(AOwner: TComponent);
begin
  CreateNew(AOwner);
  if (ClassType <> TPanelForm) {and not (csDesigning in ComponentState)} then
    if not InitInheritedComponent(Self, TPanelForm) then
      raise EResNotFound.CreateFmt(SResNotFound, [ClassName]);
  try
    if Assigned(FOnCreate) then FOnCreate(Self);
  except
    Application.HandleException(Self);
  end;
end;

constructor TPanelForm.CreateNew(AOwner: TComponent);
begin
  inherited Create(AOwner);
end;

destructor TPanelForm.Destroy;
begin
  if Assigned(FOnDestroy)
    then FOnDestroy(Self);
  inherited;
end;

end.
 unit xFormsReg;

interface

procedure Register;

implementation

uses
  DsgnIntf, Classes, PanelForm;

procedure Register;
begin
  RegisterCustomModule(TPanelForm, nil);
  RegisterClass(TPanelForm);
end;

end.
package xForms;

requires
  vcl40;

contains
  PanelForm,
  xFormsReg;

end.

There is no standard way to create such panel forms. Just create new form, change its ancestor from TForm to TPanelForm and press Alt+F12 two times.

Good luck !

Comments

This article is also

This article is also available at http://www.delphi3000.com/articles/article_490.asp

It was written before Delphi 5 introduced frames

Post new comment

The content of this field is kept private and will not be shown publicly.