Delphi與Javascript的交互


網絡上也有人寫了關於Delphi與Javascript的文章,其大多數使用ScriptControl等,均無法達到與Delphi自身融合的效果。我也是在翻閱自己的組件庫的時候發現了這個以前收集來的代碼。這個主要是使用了Mozilla的Javascript引擎,所以在程序運行的時候必須帶上js3215R.dll和msvcr70.dll這兩個動態鏈接庫。

現在我們來看一看幾個例子:

例程1:

說明:該例程主要是把Javascript代碼以字符串的形式內置在Delphi程序代碼內,然后與Delphi交互的。

unit Form;

{$I delphi.inc}

interface

uses
  Windows, Messages, SysUtils, {$IFDEF D6OR7}Variants, {$ENDIF}Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, jsintf;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FEngine: TJSEngine;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FEngine := TJSEngine.Create(40000);
  FEngine.StartDebugger;

  FEngine.Global.AddNativeObject(Edit1, 'edit');
  FEngine.Global.AddNativeObject(Button1, 'button');

  FEngine.Global.Evaluate('function toggle() { ' +
                          '  edit.Visible = ! edit.Visible;' +   // Toggle the visible property on/off
                          '  button.Caption = (edit.Visible ? "Hide" : "Show");' + // Change button
                          '}');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FEngine.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FEngine.Global.Evaluate('toggle()');
end;

end.
------------------------------------------------------------------------------------------------------
例程2:
說明:就是在例程的基礎上把Javascript代碼放置於Delphi程序外。

unit Form;

{$I delphi.inc}

interface

uses
  Windows, Messages, SysUtils, {$IFDEF D6OR7}Variants, {$ENDIF}Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, jsintf;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    FEngine: TJSEngine;
    FScript: TJSScript;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FEngine := TJSEngine.Create(40000);
  FScript := TJSScript.Create;

  FEngine.Global.AddNativeObject(Edit1, 'edit');
  FEngine.Global.AddNativeObject(Button1, 'button');

  FScript.LoadRaw('script.js'); // Load the javascript code from file
  FScript.Execute(FEngine);     // Register the function with the JS engine
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FEngine.Free;
  FScript.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FEngine.Global.Evaluate('toggle()');
end;

end.

Javascript代碼如下:
function toggle() {
  edit.Visible = ! edit.Visible;  // Toggle the visible property on/off
  button.Caption = (edit.Visible ? "Hide" : "Show");  // Change button
}

本來還有一個例程我也就不貼了,最后把所有的代碼一並打包提供下載,祝大家Enjoy Youself!
 點擊下載此文件(2.07 MB)

http://www.lsworks.net/article/56.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM