unit TestDfm2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
procedure TForm2.Button1Click(Sender: TObject);
begin
ShowMessage('Hello from Delphi!');
end;
end.
Open new project, then drop TPaxScripter, TPaxPascal, TPaxDfmConverter, 2 TButton and TMemo components on the main form and inlcude Form2 in your project.
Button1 will generate script by dfm-file which represents Form2. Memo1 will show the generated script. Button2 will show Form2 "as is":
unit TestDfm1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
PaxDfm, StdCtrls, BASE_PARSER, PaxScripter, PaxPascal;
type
TForm1 = class(TForm)
PaxDfmConverter1: TPaxDfmConverter;
Button1: TButton;
PaxScripter1: TPaxScripter;
PaxPascal1: TPaxPascal;
Memo1: TMemo;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure PaxScripter1AssignScript(Sender: TPaxScripter);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses
TestDfm2,
IMP_Controls, IMP_Graphics, IMP_StdCtrls, IMP_Forms;
procedure TForm1.Button1Click(Sender: TObject);
begin
PaxScripter1.Run;
end;
procedure TForm1.PaxScripter1AssignScript(Sender: TPaxScripter);
var
L: TStringList;
begin
// Create script from a dfm file
L := TStringList.Create;
try
with PaxDfmConverter1.UsedUnits do
begin
Add('Controls');
Add('StdCtrls');
Add('Graphics');
Add('Forms');
end;
PaxDfmConverter1.Parse('TestDfm2.dfm', L);
PaxScripter1.AddModule('1', 'paxPascal');
PaxScripter1.AddCode('1', L.Text);
finally
L.Free;
end;
// Create script-defined instance and assign event handler
with PaxScripter1 do
begin
AddCode('1', 'Form2 := TForm2.Create(nil);');
AddCode('1', 'Form2.Caption := "Generated form";');
AddCode('1', 'Form2.Button1.OnClick := Handler;');
AddCode('1', 'Form2.Show();');
AddCode('1', 'procedure Handler(Sender);');
AddCode('1', 'begin');
AddCode('1', ' print "Hello from paxScript!";');
AddCode('1', 'end;');
end;
// Show generated script
Memo1.Lines.Text := PaxScripter1.SourceCode['1'];
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Form2.Caption := 'Delphi form';
Form2.Show;
end;
end.
The generated script is:
uses
Controls,
StdCtrls,
Graphics,
Forms;
type
TForm2 = class(TForm)
Button1: TButton;
constructor Create(AOwner: TComponent);
end;
var
Form2: TForm2;
constructor TForm2.Create(AOwner: TComponent);
begin
inherited;
Button1 := TButton.Create(Self);
Button1.Name := 'Button1';
Button1.Parent := Self;
Button1.Caption := '';
with Button1 do
begin
Left := 64;
Top := 48;
Width := 75;
Height := 25;
Caption := 'Button1';
TabOrder := 0;
end;
Left := 329;
Top := 281;
Width := 327;
Height := 171;
Caption := 'Form2';
Color := clBtnFace;
Font.Charset := DEFAULT_CHARSET;
Font.Color := clWindowText;
Font.Height := -11;
Font.Name := 'MS Sans Serif';
Font.Style := [];
OldCreateOrder := False;
PixelsPerInch := 96;
end;
Form2 := TForm2.Create(nil);
Form2.Caption := "Generated form";
Form2.Button1.OnClick := Handler;
Form2.Show();
procedure Handler(Sender);
begin
print "Hello from paxScript!";
end;