RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
Classes.RegisterClass Function

Registers a class of persistent object so that it's class type can be retrieved.

Pascal
procedure RegisterClass(AClass: TPersistentClass);
C++
RegisterClass(TPersistentClass AClass);

Call RegisterClass to register a class with the streaming system. Form classes and component classes that are referenced in a form declaration (instance variables) are automatically registered. Any other classes used by an application must be explicitly registered by calling RegisterClass if instances are to be saved. 

Once classes are registered, they can be loaded or saved by the component streaming system. IdentToInt returns nil (Delphi) or NULL (C++) when passed the class name of an unregistered class, and FindClass raises an exception for unregistered classes. 

The AClass parameter is the class that is descended from TPersistent. Put the call to RegisterClass in a Register procedure. In Delphi, you can also put the call in the initialization section of the unit in which the class is defined. In C++, the call can also go in the namespace of the compilation unit that defines the class. 

If the class is already registered, RegisterClass does nothing. If a different class with the same name is already registered, RegisterClass raises an EFilerError exception.

Note: Registering a component using the RegisterNoIcon or RegisterComponents method does not automatically register the class. It is still necessary to call RegisterClass for components.
 

C++ Examples: 

 

/*
This example shows how to use the built-in component
streaming support to convert any component into a string and
convert that string back into a component.
*/

#include <memory>       //for STL auto_ptr class

class MyScrollBar : public TScrollBar
{
__published:    // IDE-managed Components
private:    // User declarations
public:     // User declarations
    __fastcall MyScrollBar(TComponent* Owner);
};

__fastcall MyScrollBar::MyScrollBar(TComponent* Owner)
    : TScrollBar(Owner)
{
}

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(MyScrollBar));
}

MyScrollBar *ScrollBar1;

AnsiString __fastcall ComponentToString(TComponent *c)
{
  AnsiString as;
  std::auto_ptr<TMemoryStream> pms(new TMemoryStream);
  std::auto_ptr<TStringStream> pss(new TStringStream(as));

  try
  {
    pms->WriteComponent(c);
    pms->Seek(0, soFromBeginning);
    ObjectBinaryToText(pms.get(), pss.get());
    pss->Seek(0, soFromBeginning);
    as = pss->DataString;
  }
  catch(...)
  {
    ShowMessage("Streaming error.");
  }

  return as;
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Memo1->Text = ComponentToString(ScrollBar1);
}

TComponent *__fastcall StringToComponent(AnsiString as)

{
  std::auto_ptr<TMemoryStream> pms(new TMemoryStream);
  std::auto_ptr<TStringStream> pss(new TStringStream(as));
  TComponent *pc;

  try
  {
    ObjectTextToBinary(pss.get(), pms.get());
    pms->Seek(0, soFromBeginning);
  }
  catch(...)
  {
    ShowMessage("Streaming error.");
  }

  pc = pms->ReadComponent(NULL);
  return pc;
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  TComponent *temp = StringToComponent(Memo1->Text);
  ScrollBar1->Free();

  ScrollBar1 = dynamic_cast<MyScrollBar *>(temp);
  ScrollBar1->Parent = Form1;
  ScrollBar1->Visible = TRUE;
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  ScrollBar1 = new MyScrollBar(Form1);  // Form1 will clean up the scroll bar.
  ScrollBar1->Parent = Form1;
  ScrollBar1->Visible = TRUE;
  ScrollBar1->Top = 48;
  ScrollBar1->Left = 250;
  ScrollBar1->Name = "Ricksbar";
}

 

Delphi Examples: 

{
This example shows how to use the built-in component
streaming support to convert any component into a string and
convert that string back into a component.
}
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  MyScrollBar = class(TScrollBar)
  end;

var
  Form1: TForm1;
  ScrollBar1: MyScrollBar;
  
implementation

{$R *.dfm}

function ComponentToStringProc(Component: TComponent): string;
var
  BinStream:TMemoryStream;
  StrStream: TStringStream;
  s: string;
begin
  BinStream := TMemoryStream.Create;
  try
    StrStream := TStringStream.Create(s);
    try
      BinStream.WriteComponent(Component);
      BinStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(BinStream, StrStream);
      StrStream.Seek(0, soFromBeginning);
      Result:= StrStream.DataString;
    finally
      StrStream.Free;
    end;
  finally
    BinStream.Free
  end;
end;

function StringToComponentProc(Value: string): TComponent;
var
  StrStream:TStringStream;
  BinStream: TMemoryStream;
begin
  StrStream := TStringStream.Create(Value);
  try
    BinStream := TMemoryStream.Create;
    try
      ObjectTextToBinary(StrStream, BinStream);
      BinStream.Seek(0, soFromBeginning);
      Result:= BinStream.ReadComponent(nil);
    finally
      BinStream.Free;
    end;
  finally
    StrStream.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Text:= ComponentToStringProc(ScrollBar1);
end;

// Edit the scrollbar parameters in the memo, click this
// button and see the scrollbar change.
procedure TForm1.Button2Click(Sender: TObject);
begin
  ScrollBar1.Free;
  ScrollBar1:= (StringToComponentProc(Memo1.Text) as MyScrollBar);
  ScrollBar1.Parent:= Form1;
  ScrollBar1.Visible:= TRUE;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ScrollBar1:= MyScrollBar.Create(Form1);
  ScrollBar1.Parent:= Form1;
  ScrollBar1.Visible:= TRUE;
  ScrollBar1.Top:= 48;
  ScrollBar1.Left:= 250;
  ScrollBar1.Name:= 'Ricksbar';
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ScrollBar1.Free;
end;

initialization

RegisterClass(MyScrollBar);

 

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