dcef3 基本使用经验总结 dcef3 基本使用经验总结

dcef3 基本使用经验总结

https://www.cnblogs.com/h2285409/p/10517483.html

加载命令启动参数    --enable-system-flash 会加载系统默认的flash浏览器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
procedure  OnBeforeCmdLine(const processType: ustring; const commandLine: ICefCommandLine);
begin
   commandLine . AppendSwitch( '--enable-system-flash' );
   commandLine . AppendSwitch( '--disable-web-security' );
   commandLine . AppendSwitch( 'no-proxy-server' );
end ;
 
begin
   CefCache :=  'cache' ;
   CefLocale :=  'zh-CN' ;
   CefOnBeforeCommandLineProcessing := OnBeforeCmdLine;
   CefSingleProcess :=  False ;
 
   if  not CefLoadLibDefault then
     Exit;
 
   Application . Initialize;
   Application . CreateForm(TMainForm, MainForm);
   Application . Run;
end .

  

其它cmdLine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
procedure  OnBeforeCmdLine(const processType: ustring; const commandLine: ICefCommandLine);
begin
 
   //加载系统安装的flash,使用前系统需安装flash播放器
   //commandLine.AppendSwitch('--enable-system-flash');
 
   //允许js跨域
   commandLine . AppendSwitch( '--disable-web-security' ); 
 str_ip:='218.207.212.79:80';   //设置代理ip  120.203.159.18:8118  218.189.26.20:8080
  commandLine.AppendSwitchWithValue('proxy-server',str_ip);
 
   //禁用代理
   //commandLine.AppendSwitch('no-proxy-server');
 
   //不同域名不同进程
   commandLine . AppendSwitch( '--process-per-site' );
 
   //指定子进程渲染
   //commandLine.AppendSwitchWithValue('browser-subprocess-path', 'cefsubprocess.exe');
 
   //加载指定flash版本
   commandLine . AppendSwitchWithValue( 'ppapi-flash-version' ,  '21.0.0.213' );
   commandLine . AppendSwitchWithValue( 'ppapi-flash-path' ,  'PepperFlash\pepflashplayer.dll' );
end ;

  

拓展JS方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
TCustomRenderProcessHandler =  class (TCefRenderProcessHandlerOwn)
   protected
     procedure  OnWebKitInitialized; override;
   end ;
 
TTestExtension =  class
class  function hello: string;
class  procedure mouseover(const data: string);
end ;
 
procedure  TCustomRenderProcessHandler.OnWebKitInitialized;
begin
{$IFDEF DELPHI14_UP}
   TCefRTTIExtension . Register( 'app' , TTestExtension);
{ $ENDIF }
end ;
 
class  procedure TTestExtension.mouseover(const data: string);
var
msg: ICefProcessMessage;
begin
msg := TCefProcessMessageRef . New( 'mouseover' );
msg . ArgumentList . SetString( 0 , data);
TCefv8ContextRef . Current . Browser . SendProcessMessage(PID_BROWSER, msg);
end ;
 
class  function TTestExtension.hello: string;
begin
Result :=  'Hello from Delphi' ;
end ;
 
initialization
   CefRemoteDebuggingPort :=  9000 ;
   CefRenderProcessHandler := TCustomRenderProcessHandler . Create;
   CefBrowserProcessHandler := TCefBrowserProcessHandlerOwn . Create;

  

Delphi执行JS方法

1
2
3
4
5
6
procedure  TMainForm.actExecuteJSExecute(Sender: TObject);
begin
   if  crm.Browser <> nil then
     crm . Browser . MainFrame . ExecuteJavaScript(
       'alert(' 'JavaScript execute works!' ');' ,  'about:blank' ,  0 );
end ;

 

一般单进程模式是用来调试的,release版本最好是多进程模式,如果debug版本也是多进程的话,由于Browser、Rendder等进程是独立分开的,所以即便在一些函数中打上断点也进不去!而单进程话就都可以进去。至于debug版本多进程模式下为何会弹出两个主程序窗口我也不是很清楚,但是release版本多进程模式下就正常了,只有一个主程序窗口。并且多进程模式下调用CefShutdown就是OK的,app也能够正常析构,而单进程模式调用CefShutdown会直接崩溃!

  另外,官方文档上说release版本单进程不是很稳定,不建议使用单进程模式

加载命令启动参数    --enable-system-flash 会加载系统默认的flash浏览器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
procedure  OnBeforeCmdLine(const processType: ustring; const commandLine: ICefCommandLine);
begin
   commandLine . AppendSwitch( '--enable-system-flash' );
   commandLine . AppendSwitch( '--disable-web-security' );
   commandLine . AppendSwitch( 'no-proxy-server' );
end ;
 
begin
   CefCache :=  'cache' ;
   CefLocale :=  'zh-CN' ;
   CefOnBeforeCommandLineProcessing := OnBeforeCmdLine;
   CefSingleProcess :=  False ;
 
   if  not CefLoadLibDefault then
     Exit;
 
   Application . Initialize;
   Application . CreateForm(TMainForm, MainForm);
   Application . Run;
end .

  

其它cmdLine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
procedure  OnBeforeCmdLine(const processType: ustring; const commandLine: ICefCommandLine);
begin
 
   //加载系统安装的flash,使用前系统需安装flash播放器
   //commandLine.AppendSwitch('--enable-system-flash');
 
   //允许js跨域
   commandLine . AppendSwitch( '--disable-web-security' ); 
 str_ip:='218.207.212.79:80';   //设置代理ip  120.203.159.18:8118  218.189.26.20:8080
  commandLine.AppendSwitchWithValue('proxy-server',str_ip);
 
   //禁用代理
   //commandLine.AppendSwitch('no-proxy-server');
 
   //不同域名不同进程
   commandLine . AppendSwitch( '--process-per-site' );
 
   //指定子进程渲染
   //commandLine.AppendSwitchWithValue('browser-subprocess-path', 'cefsubprocess.exe');
 
   //加载指定flash版本
   commandLine . AppendSwitchWithValue( 'ppapi-flash-version' ,  '21.0.0.213' );
   commandLine . AppendSwitchWithValue( 'ppapi-flash-path' ,  'PepperFlash\pepflashplayer.dll' );
end ;

  

拓展JS方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
TCustomRenderProcessHandler =  class (TCefRenderProcessHandlerOwn)
   protected
     procedure  OnWebKitInitialized; override;
   end ;
 
TTestExtension =  class
class  function hello: string;
class  procedure mouseover(const data: string);
end ;
 
procedure  TCustomRenderProcessHandler.OnWebKitInitialized;
begin
{$IFDEF DELPHI14_UP}
   TCefRTTIExtension . Register( 'app' , TTestExtension);
{ $ENDIF }
end ;
 
class  procedure TTestExtension.mouseover(const data: string);
var
msg: ICefProcessMessage;
begin
msg := TCefProcessMessageRef . New( 'mouseover' );
msg . ArgumentList . SetString( 0 , data);
TCefv8ContextRef . Current . Browser . SendProcessMessage(PID_BROWSER, msg);
end ;
 
class  function TTestExtension.hello: string;
begin
Result :=  'Hello from Delphi' ;
end ;
 
initialization
   CefRemoteDebuggingPort :=  9000 ;
   CefRenderProcessHandler := TCustomRenderProcessHandler . Create;
   CefBrowserProcessHandler := TCefBrowserProcessHandlerOwn . Create;

  

Delphi执行JS方法

1
2
3
4
5
6
procedure  TMainForm.actExecuteJSExecute(Sender: TObject);
begin
   if  crm.Browser <> nil then
     crm . Browser . MainFrame . ExecuteJavaScript(
       'alert(' 'JavaScript execute works!' ');' ,  'about:blank' ,  0 );
end ;

 

一般单进程模式是用来调试的,release版本最好是多进程模式,如果debug版本也是多进程的话,由于Browser、Rendder等进程是独立分开的,所以即便在一些函数中打上断点也进不去!而单进程话就都可以进去。至于debug版本多进程模式下为何会弹出两个主程序窗口我也不是很清楚,但是release版本多进程模式下就正常了,只有一个主程序窗口。并且多进程模式下调用CefShutdown就是OK的,app也能够正常析构,而单进程模式调用CefShutdown会直接崩溃!

  另外,官方文档上说release版本单进程不是很稳定,不建议使用单进程模式

猜你喜欢

转载自www.cnblogs.com/delphi-xe5/p/11058695.html