home *** CD-ROM | disk | FTP | other *** search
/ PC Format Collection 48 / SENT14D.ISO / tech / delphi / disk15 / dynainst.pak / DYNAMAIN.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1995-08-24  |  2.5 KB  |  89 lines

  1. unit Dynamain;
  2.  
  3. { This unit demonstrates the use of class references to dynamically
  4.   instantiate components. }
  5.  
  6. interface
  7.  
  8. uses
  9.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  10.   Forms, Dialogs, ExtCtrls, StdCtrls;
  11.  
  12. type
  13.   TForm1 = class(TForm)
  14.     CreationArea: TPanel;
  15.     Panel1: TPanel;
  16.     GroupBox1: TGroupBox;
  17.     Label1: TLabel;
  18.     Label2: TLabel;
  19.     Label3: TLabel;
  20.     Label4: TLabel;
  21.     ClassList: TComboBox;
  22.     XPos: TEdit;
  23.     YPos: TEdit;
  24.     Button1: TButton;
  25.     TextProp: TEdit;
  26.     procedure FormCreate(Sender: TObject);
  27.     procedure Button1Click(Sender: TObject);
  28.   private
  29.     { Private declarations }
  30.   public
  31.     { Public declarations }
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.  
  37. implementation
  38.  
  39. {$R *.DFM}
  40.  
  41. { In the form's OnCreate method, we fill the combo box with a list
  42.   of class names, each accompanied by an actual class reference
  43.   (stored in Items.Objects).  }
  44.  
  45. procedure TForm1.FormCreate(Sender: TObject);
  46. begin
  47.   with ClassList.Items do
  48.   begin
  49.     AddObject('TEdit', TObject(TEdit));
  50.     AddObject('TCheckBox', TObject(TCheckBox));
  51.     AddObject('TRadioButton', TObject(TRadioButton));
  52.     AddObject('TButton', TObject(TButton));
  53.     AddObject('TListBox', TObject(TListBox));
  54.   end;
  55.   ClassList.ItemIndex := 0;
  56. end;
  57.  
  58. { When the Create button is pressed, we fetch the class reference from
  59.   the currently selected itm in the combo box.  We can then create an
  60.   actual instance of whatever class the reference points to by calling its
  61.   constructor.  We then parent the new instance to a panel, and position it
  62.   according to the values in the edit boxes.
  63.  
  64.   Lastly, we send a WM_SETTEXT message to the control (as long as it is a
  65.   TWinControl) and allow the control to handle the message however it wants.
  66.   (Some controls have a Caption property, while others have a Text property.
  67.   Sending a WM_SETTEXT message avoids having to know which property the new
  68.   instance has.)  }
  69.  
  70. procedure TForm1.Button1Click(Sender: TObject);
  71. var
  72.   Reference: TControlClass;
  73.   Instance: TControl;
  74.   Text: array[0..255] of Char;
  75. begin
  76.   Reference := TControlClass(ClassList.Items.Objects[ClassList.ItemIndex]);
  77.  
  78.   Instance := Reference.Create(Self);
  79.   Instance.Parent := CreationArea;
  80.   Instance.Left := StrToInt(XPos.Text);
  81.   Instance.Top := StrToInt(YPos.Text);
  82.  
  83.   if Instance is TWinControl then
  84.     SendMessage(TWinControl(Instance).Handle, WM_SETTEXT, 0,
  85.       Longint(StrPCopy(Text, TextProp.Text)));
  86. end;
  87.  
  88. end.
  89.