unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,ComObj,ActiveX; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } m_OLE : Variant; end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var tfname:string; StrList: TStringList; begin //创建JS变体类型 m_OLE := CreateOleObject(‘ScriptControl‘); m_OLE.Language:=‘JavaScript‘; StrList:= Tstringlist.Create; try //读取同目录下的JS文件 StrList.LoadFromFile(ExtractFilePath(ParamStr(0)) + ‘Date.js‘); //将读取的文件加载至 m_OLE 中 m_OLE.AddCode(strlist.Text); finally StrList.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); var DateStr : string; begin CoInitialize(nil); //线程中调用Variant 需要使用 CoInitialize,CoUnInitialize DateStr := m_OLE.Eval(‘Now()‘); //调用 JS中now() 方法获取当前时间 Memo1.Lines.Add(DateStr); DateStr := m_OLE.Eval(‘DateFormat("YEAR")‘); //传入YEAR 调用 JS中DateFormat() 方法获取当前时间 Memo1.Lines.Add(DateStr); CoUnInitialize; end; end.
时间: 2024-10-05 08:24:24