tms脚本演示代码之一

本例演示:宿主程序(exe)只有界面的设计,业务规则全部放在脚本文件里面。这个脚本文件是个纯文本文件,可以随意修改。一旦运行宿主程序就会装载脚本文件里面的脚本形式的业务规则。这样,业务规则很容易被修改。

 

宿主单元:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, atScript, atPascal, StdCtrls, GridsEh, DBGridEh, DB,
  ADODB, ExtCtrls, DBCtrls, Buttons;

type
  TForm1 = class(TForm)
    scripter: TatPascalScripter;
    DataSource1: TDataSource;
    ADOTable1: TADOTable;
    DBGridEh1: TDBGridEh;
    DBNavigator1: TDBNavigator;
    Panel1: TPanel;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses ap_Classes;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Scripter.AddComponents(Self);                  // 让脚本可以访问宿主的对象

  scripter.AddComponents(DataSource1);
  scripter.AddComponents(ADOTable1);
  scripter.AddComponents(DBGridEh1);
  scripter.AddComponents(DBNavigator1);
  scripter.AddComponent(Panel1);
  Scripter.AddEnumeration(TypeInfo(TAlign));     // 增加支持枚举型

  Scripter.AddLibrary(TatClassesLibrary);           // 增加支持TNotifyEvent
  scripter.SourceCode.LoadFromFile(ExtractFilePath(Application.ExeName) + '1.txt');   // 装载脚本文件
  scripter.ExecuteSubroutine('GetDataSet');   // 执行脚本里面的方法
  scripter.ExecuteSubroutine('SetEvent');
end;

end.

 

脚本文件(1.txt):

procedure GetDataSet;
begin
  AdoTable1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb;Persist Security Info=False';
  AdoTable1.TableName := 't1';
  AdoTable1.Active := True;
  DBGridEh1.DataSource := DataSource1;
  DataSource1.DataSet := AdoTable1;
  DBNavigator1.DataSource := DataSource1;
  DBNavigator1.flat := true;
  DBGridEh1.flat := true;
  dbgrideh1.align := alclient;
end;

procedure Panel1Click(Sender);
begin
  ShowMessage('welcome to use scripter');
end;

procedure SetEvent;
begin
  panel1.Caption := 'welcome';
  panel1.onClick := 'Panel1Click';
end;

 

宿主窗体:

object Form1: TForm1
  Left = 338
  Top = 170
  Width = 511
  Height = 379
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object DBGridEh1: TDBGridEh
    Left = 64
    Top = 57
    Width = 329
    Height = 216
    Align = alCustom
    Flat = False
    FooterColor = clWindow
    FooterFont.Charset = DEFAULT_CHARSET
    FooterFont.Color = clWindowText
    FooterFont.Height = -11
    FooterFont.Name = 'MS Sans Serif'
    FooterFont.Style = []
    ImeName = 'ÖÐÎÄ (¼òÌå) - Ëѹ·Îå±ÊÊäÈë·¨'
    TabOrder = 0
    TitleFont.Charset = DEFAULT_CHARSET
    TitleFont.Color = clWindowText
    TitleFont.Height = -11
    TitleFont.Name = 'MS Sans Serif'
    TitleFont.Style = []
  end
  object DBNavigator1: TDBNavigator
    Left = 0
    Top = 0
    Width = 503
    Height = 25
    Align = alTop
    TabOrder = 1
  end
  object Panel1: TPanel
    Left = 0
    Top = 306
    Width = 503
    Height = 41
    Align = alBottom
    Caption = 'Panel1'
    TabOrder = 2
  end
  object scripter: TatPascalScripter
    SaveCompiledCode = False
    ShortBooleanEval = False
    LibOptions.SearchPath.Strings = (
      '$(CURDIR)'
      '$(APPDIR)')
    LibOptions.SourceFileExt = '.psc'
    LibOptions.CompiledFileExt = '.pcu'
    LibOptions.UseScriptFiles = False
    Left = 48
    Top = 56
  end
  object DataSource1: TDataSource
    Left = 88
    Top = 56
  end
  object ADOTable1: TADOTable
    Left = 128
    Top = 56
  end
end

posted @ 2010-05-13 07:42  delphi中间件  阅读(770)  评论(0编辑  收藏  举报