OldCEF4Delphi 触发线程消息

//具体的例子从源码的 demo javascript 中看使用用例
// JS绑定delphi的方法
//procedure(const browser: ICefBrowser; const frame: ICefFrame; const context: ICefv8Context)
procedure TfrmOutSideMain.GlobalCEFApp_OnContextCreated(const browser: ICefBrowser;
  const frame: ICefFrame; const context: ICefv8Context);
var
  cefHandler: ICefv8Handler;
  cefFunction: ICefv8Value;
begin
  cefHandler := TMyV8Handler.Create;

  //关闭退出
  cefFunction := TCefv8ValueRef.NewFunction('ResClose', cefHandler);
  context.Global.SetValueByKey('ResClose', cefFunction, V8_PROPERTY_ATTRIBUTE_NONE);
  //浏览器调用系统的微软的TTS语音
 //网页调用调用方法 MSSpeechVoice('请xx到诊室就诊',40,0,'js的回调方法')
 cefFunction := TCefv8ValueRef.NewFunction('MSSpeechVoice', cefHandler);
 context.Global.SetValueByKey('MSSpeechVoice', cefFunction, V8_PROPERTY_ATTRIBUTE_NONE);
 
end;

--------------------------------------------------------------------------
unit uMyV8Handler;

{$I cef.inc}

interface

uses
  uCEFTypes, uCEFInterfaces, uCEFv8Value, uCEFProcessMessage, uCEFv8Handler,
  uCEFv8Context, Windows, Messages, SysUtils,Dialogs,uSysLogClass;

type
  TMyV8Handler = class(TCefv8HandlerOwn)
  protected
    FMyParam: string;
    function Execute(const name: ustring; const obj: ICefv8Value; const arguments: TCefv8ValueArray;
      var retval: ICefv8Value; var exception: ustring): Boolean; override;
  end;

implementation

function TMyV8Handler.Execute(const name: ustring; const obj: ICefv8Value; const arguments: TCefv8ValueArray;
  var retval: ICefv8Value; var exception: ustring): Boolean;
var
  cefPromsg: ICefProcessMessage;
 // CefFrame: ICefFrame;
  iHandle: THandle;
  sResult: string;
  SpeedStr,Volume,Speed,SpeakCompletedCallName:String;
begin
  Result := False;

  try
    if name = 'ResClose' then
    begin
      iHandle := FindWindow(PChar('TfrmOutSideMain'), nil);
      PostMessage(iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);
      Result := True;
    end
    else if name = 'MSSpeechVoice' then
    begin
      if Length(arguments) = 4 then
      begin

        cefPromsg := TCefProcessMessageRef.New(name);
        cefPromsg.ArgumentList.SetString(0, arguments[0].GetStringValue);
        cefPromsg.ArgumentList.SetString(1, arguments[1].GetStringValue);
        cefPromsg.ArgumentList.SetString(2, arguments[2].GetStringValue);
        cefPromsg.ArgumentList.SetString(3, arguments[3].GetStringValue);
        //老版本的在Browser上 触发后,会进入chromum1.OnProcessMessageReceived 事件中
        //具体的业务中实现调用语音
        TCefv8ContextRef.Current.Browser.SendProcessMessage(PID_BROWSER, cefPromsg);
        //新版本的为cef4delphi最新版本为
        {
        CefFrame := TCefv8ContextRef.Current.Browser.MainFrame;
        if (CefFrame <> nil) and CefFrame.IsValid then
        begin
          CefFrame.SendProcessMessage(PID_BROWSER, cefPromsg);
        end;
        }
      end;

      sResult := '调用成功';
      retval := TCefv8ValueRef.NewString(sResult);
      Result := True;
    end
    else if name = 'ResPrint' then
    begin
      retval := TCefv8ValueRef.NewString(sResult);
      Result := True;
    end;
  finally
    cefPromsg := nil;
  end;
end;

end.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值