ClassesGetClass (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example allows you to type the name of a graphics class into an edit control. 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.

Code

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));  // I works as well.
    Edit1.Text := GraphicExtension(mygraphclass);
  except
    on EAccessViolation do
      MessageDlg('Invalid class name.', mtError, [mbOK], 0)
  end;
end;

Uses