Logo New book: Delphi 2007 Handbook
My blog in online
Delphi tech support service: support.marcocantu.com
Google
  Web www.marcocantu.com

Menu for Development

Site Menu
Delphi 2007 Handbook
Mastering Borland Delphi 2005
Essential Pascal
Essential Delphi
Buy Books Online
Code Repository
Newsgroups
White Papers
Tools
Conferences
Training
Delphi Links
Contact Marco

My Other Sites
Italian Site (www.marcocantu.it)
Developers Newsgroups Browser (dev.newswhat.com)
My town (www.piazzacavalli.net)
the delphi search
Wintech Italia (my company)

Breaking News
Buy Mastering Borland Delphi 2005 from Amazon
Free ebook: Mastering Delphi Update for Delphi 2006

Advertising
Home My Blog Books My Bookstore Development Links Marco


Home: Code Repository: Mastering Delphi 6

Chapter 03 - Project ClassRef

Project Structure

ClassRef.dpr
program ClassRef;

uses
  Forms,
  CRefForm in 'CRefForm.pas' {Form1};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
CRefForm.pas
unit CRefForm;

interface

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

type
  // class reference type (redeclaration)
  TControlClass = class of TControl;

  TForm1 = class(TForm)
    Panel1: TPanel;
    RbtnRadio: TRadioButton;
    RbtnButton: TRadioButton;
    RbtnEdit: TRadioButton;
    procedure RbtnRadioClick(Sender: TObject);
    procedure RbtnButtonClick(Sender: TObject);
    procedure RbtnEditClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    ClassRef: TControlClass;
    Counter: Integer;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.RbtnRadioClick(Sender: TObject);
begin
  ClassRef := TRadioButton;
end;

procedure TForm1.RbtnButtonClick(Sender: TObject);
begin
  ClassRef := TButton;
end;

procedure TForm1.RbtnEditClick(Sender: TObject);
begin
  ClassRef := TEdit;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ClassRef := TRadioButton;
end;

procedure TForm1.FormMouseDown(
  Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  NewCtrl: TControl;
  MyName: String;
begin
  // create the control
  NewCtrl := ClassRef.Create (Self);
  // hide it temporarily, to avoid flickering
  NewCtrl.Visible := False;
  // set parent and position
  NewCtrl.Parent := Self;
  NewCtrl.Left := X;
  NewCtrl.Top := Y;
  // compute the unique name (and caption)
  Inc (Counter);
  MyName := ClassRef.ClassName + IntToStr (Counter);
  Delete (MyName, 1, 1);
  NewCtrl.Name := MyName;
  // now show it
  NewCtrl.Visible := True;
end;


end.
CRefForm.dfm
object Form1: TForm1
  Left = 183
  Top = 148
  Width = 474
  Height = 344
  Caption = 'Component Builder'
  Color = clBtnFace
  Font.Charset = ANSI_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  OnCreate = FormCreate
  OnMouseDown = FormMouseDown
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 466
    Height = 33
    Align = alTop
    TabOrder = 0
    object RbtnRadio: TRadioButton
      Left = 24
      Top = 8
      Width = 89
      Height = 17
      Caption = 'Radio Button'
      Checked = True
      TabOrder = 0
      TabStop = True
      OnClick = RbtnRadioClick
    end
    object RbtnButton: TRadioButton
      Left = 144
      Top = 8
      Width = 61
      Height = 17
      Caption = 'Button'
      TabOrder = 1
      OnClick = RbtnButtonClick
    end
    object RbtnEdit: TRadioButton
      Left = 240
      Top = 8
      Width = 49
      Height = 17
      Caption = 'Edit'
      TabOrder = 2
      OnClick = RbtnEditClick
    end
  end
end