在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论