吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 950|回复: 0
收起左侧

[求助] Memo1里扔啥代码能正常运行?

[复制链接]
冥界3大法王 发表于 2021-5-15 13:02
[Asm] 纯文本查看 复制代码
unit Unit3;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TForm3 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    m_OLE: Variant;
    { Public declarations }
  end;

var
  Form3: TForm3;

implementation

{$R *.dfm}

uses
  ComObj;

function RunJs(const JsCode, JsVar: string): string;
var
  script: OleVariant;
begin
  try
    script := CreateOleObject('ScriptControl');
    script.Language := 'JavaScript';
    script.ExecuteStatement(JsCode);
    Result := script.Eval(JsVar);
  except
    Result := '';
  end;
end;

procedure TForm3.Button1Click(Sender: TObject);
var
  OpenDlg: TOpenDialog;
  FileName: string;
begin
  OpenDlg := TOpenDialog.Create(nil);
  try
    OpenDlg.Filter := 'JavaScript脚本|*.*';
    OpenDlg.DefaultExt := '*.*';
    if OpenDlg.Execute then
    begin
      FileName := Trim(OpenDlg.FileName);
      if FileName <> '' then
      begin
        Memo1.Lines.LoadFromFile(FileName, TEncoding.UTF8);
      end;
    end;
  finally
    FreeAndNil(OpenDlg);
  end;
end;

procedure TForm3.Button2Click(Sender: TObject);
var
  s: string;
begin
  s := RunJs(Memo1.Text, 'str');
  ShowMessage(s);
end;

procedure TForm3.Button3Click(Sender: TObject);
var
  js: OleVariant;
begin
  js := CreateOleObject('ScriptControl'); //创建组件
  js.Language := 'JavaScript'; //指定组件所使用的语言,也可以是VBScript
  ShowMessage(js.Eval('100+1')); //计算100+1的值
  js := Unassigned;
end;

end.

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 04:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表