RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
Classes.RegisterClasses Function

Registers a set of classes.

Pascal
procedure RegisterClasses(AClasses: array of TPersistentClass);
C++
RegisterClasses(array of TPersistentClass AClasses);

Call RegisterClasses to register a set of custom classes in a single line. Each class is registered by calling RegisterClass. Unregistered classes can't be loaded or saved by the component streaming system.

Note: In C++, the AClasses parameter identifies the classes to be registered These must be descendants of TPersistent. The AClasses_Size parameter indicates the index of the last class in AClasses (one less than the total number of classes).
 

C++ Examples: 

 

/*
The following example allows users to type the name of a
graphics class into an edit control and when a button is
pressed, the name of the default extension for that class is
displayed in another edit control.  In the form’s OnCreate
event handler (or a similar place) you must register the
graphics classes so that GetClass can find them.
*/
#include <memory>       //for STL auto_ptr class

TMetaClass *MetaClass;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  std::auto_ptr<TClassFinder> myClassFinder(new TClassFinder(MetaClass, False));
  TGraphicClass mygraphclass = (TGraphicClass) (myClassFinder->GetClass(Edit2->Text));
//  TGraphicClass mygraphclass = (TGraphicClass) GetClass(Edit2->Text);
  Edit1->Text = GraphicExtension(mygraphclass);
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  MetaClass = __classid(TIcon); // ico
  RegisterClasses(&MetaClass, 0);
  MetaClass = __classid(Graphics::TBitmap); // bmp
  RegisterClasses(&MetaClass, 0);
  MetaClass = __classid(TMetafile); // emf
  RegisterClasses(&MetaClass, 0);
  MetaClass = __classid(TShape);  // nographic extension
  RegisterClasses(&MetaClass, 0);
  MetaClass = __classid(TImage); // nographic extension
  RegisterClasses(&MetaClass, 0);
}
/*
ReadComponentResFile, WriteComponentResFile example
The following code defines a component class (TStreamable
class) and a persistent class (TContainedClass) that is the
type of a property on the component class. When a button is
clicked, an instance of the component class is created,
saved to a file, deleted, and then loaded from the file. By
setting a breakpoint on the property setter of the
TContainedClass SomeData property, you can watch how the
streaming system sets property values when it reads them
from a file.
*/
class TContainedClass : public TPersistent
{
  private:
    int FSomeData;
    void __fastcall SetSomeData(int Value)
      {
        // Place a breakpoint here and notice
        // how the data is streamed back.
        FSomeData = Value;
      }
  public:
    virtual __fastcall TContainedClass(void) : TPersistent()
      { FSomeData = 42; }
  // Only published properties
  // are automatically streamed.
  __published:
    __property int SomeData = { read=FSomeData, write=SetSomeData };
};

class TStreamableClass : public TComponent
{
  private:
    TContainedClass* FContainedClass;
  // Only published properties
  // are automatically streamed.
  __published:
    __property TContainedClass* ContainedClass = { read= FContainedClass, write=FContainedClass };
  public:
    virtual __fastcall TStreamableClass(TComponent* Owner) : TComponent(Owner)
      {
        FContainedClass = new TContainedClass();
      }

    virtual __fastcall ~TStreamableClass(void)
      {
        delete FContainedClass;
      }
};

void RegisterClassesWithStreamingSystem(void)
{
  // Make sure that as part of the startup
  // code our streaming classes are registered
  // with the streaming system.
  #pragma startup RegisterClassesWithStreamingSystem

  Classes::RegisterClass(__classid(TContainedClass));
  Classes::RegisterClass(__classid(TStreamableClass));
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  const AnsiString FileName = GetCurrentDir() + "\\..\\Test";

  TStreamableClass* AClassInstance;

  AClassInstance = new TStreamableClass(NULL);
  WriteComponentResFile(FileName, AClassInstance);
  delete AClassInstance;

  TComponent* temp = ReadComponentResFile(FileName, NULL);
  AClassInstance = static_cast<TStreamableClass*>(temp);
  delete AClassInstance;
}

 

Delphi Examples: 

{
The following example allows users to type the name of a
graphics class into an edit control and when a button is
pressed, the name of the default extension for that class is
displayed in another edit control.  In the form’s OnCreate
event handler (or a similar place) you must register the
graphics classes so that GetClass can find them.
}

uses ExtCtrls;

procedure TForm1.Button1Click(Sender: TObject);
var
  myClassFinder: TClassFinder;
  mygraphclass: TGraphicClass;
begin
  { make sure the classes are registered so that GetClass will work -- }
  { Usually, this goes in the initialization section where it is only executed once }
  RegisterClasses([TIcon, TBitmap, TButton, TShape, TImage, TMetafile]);
  try
    myClassFinder := TClassFinder.Create(TIcon, False);
    mygraphclass := TGraphicClass(myClassFinder.GetClass(Edit2.Text));
//    mygraphclass := TGraphicClass(GetClass(Edit2.Text));  // works as well
    Edit1.Text := GraphicExtension(mygraphclass);
  except
    on EAccessViolation do
      MessageDlg('Invalid class name.', mtError, [mbOK], 0)
  end;
end;
{
The following code defines a component class (TStreamable
class) and a persistent class (TContainedClass) that is the
type of a property on the component class. When a button is
clicked, an instance of the component class is created, saved
to a file, deleted, and then loaded from the file. By setting
a breakpoint on the property setter of the TContainedClass
SomeData property, you can watch how the streaming system
sets property values when it reads them from a file.
The following unit defines the classes to stream:
} 

{$R-,T-,H+,X+}

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TContainedClass = class(TPersistent)
  private
    FSomeData: Integer;
    procedure SetSomeData(Value: Integer);
  public
    constructor Create;
    // Only published properties
    // are automatically streamed.
  published
    property SomeData: Integer read FSomeData write SetSomeData;
  end;

  TStreamableClass = class(TComponent)
  private
    FContainedClass: TContainedClass;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    // Only published properties
    // are automatically streamed.
  published
    property ContainedClass: TContainedClass read FContainedClass write FContainedClass;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TContainedClass.SetSomeData(Value: Integer);
begin
{ Place a breakpoint here and notice how the data is streamed back. }
  FSomeData := Value;
end;

constructor TContainedClass.Create;
begin
  FSomeData := 42;
end;

constructor TStreamableClass.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FContainedClass := TContainedClass.Create;
end;

destructor TStreamableClass.Destroy;
begin
  FContainedClass.Free;
end;

{
The following OnClick event handler should be added to a
button on the application’s main form. It causes the above
classes to be streamed out and in.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  AClassInstance: TStreamableClass;
begin
  AClassInstance := TStreamableClass.Create(nil);
  WriteComponentResFile('Test', AClassInstance);
  FreeAndNil(AClassInstance);

  AClassInstance := Classes.ReadComponentResFile('Test', nil) as TStreamableClass;
  FreeAndNil(AClassInstance);
end;

initialization
  RegisterClasses([TContainedClass, TStreamableClass]);

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!