随着Chrome浏览器的使用范围越来越多,而且Chrome不挑食,基本上什么系统都可以跑,而且速度那是飞快,所以C/S程序调用第三方的B/S程序或者网页时都考虑使用支持最新H5技术的内核,而Delphi的浏览控件那真心是不支持很多新的东西,而且还是微软的内核,各种的水士不符,没有办法只能想其他办法。
经过寻找最终发现CEF4delphi项目,CEF4Delphi 是由 SalvadorDíazFau 创建的一个开源项目,用于在基于Delphi的应用程序中嵌入基于Chromium的浏览器。CEF4Delphi 基于Henri Gourvest 公司的 DCEF3。DCEF3的原始许可证仍适用于CEF4Delphi。阅读任何* .pas文件的第一行中的许可条款。
控件的下载地址:https://github.com/salvadordf/CEF4Delphi/archive/master.zip
软件编译后的运行环境下载:http://opensource.spotify.com/cefbuilds/index.html
说明一下运行环境,由于是调用的Chrome内核,所以就有一堆的dll文件要与Chrome进行交互,程序编译后如果没有这些文件支持,就会如下的提示
除了准备以上的材料以外,安装最新版的Chrom也是必须的,这样我们就可以开始进入调用的过程了
1.安装控件
将下载下来的CEF4Delphi-master.zip文件解压到Delphi目录下,例如:
然后将程序的source路径加入到library路径中,同时要注意32位、64位的问题
打开控件的安装文件
然后编译安装,我们的控件部分就安装成功了
然后就可以打开DEMO进行学习使用了,
这里面有几点要注意,就是这个控件的项目文件与普通的项目文件是有区别的
program Project2;uses{$IFDEF DELPHI16_UP}Vcl.Forms,WinApi.Windows,System.SysUtils,{$ELSE}Forms,Windows,SysUtils,{$ENDIF }uCEFApplication,uCEFWorkScheduler,Unit2 in 'Unit2.pas' {SimpleExternalPumpBrowserFrm};{$R *.res}beginGlobalCEFWorkScheduler := TCEFWorkScheduler.Create(nil);GlobalCEFApp := TCefApplication.Create;GlobalCEFApp.FlashEnabled := False;GlobalCEFApp.FastUnload := True;GlobalCEFApp.ExternalMessagePump := True;GlobalCEFApp.MultiThreadedMessageLoop := False;GlobalCEFApp.OnScheduleMessagePumpWork := GlobalCEFApp_OnScheduleMessagePumpWork;if GlobalCEFApp.StartMainProcess thenbeginApplication.Initialize;{$IFDEF DELPHI11_UP}Application.MainFormOnTaskbar := True;{$ENDIF}Application.CreateForm(TSimpleExternalPumpBrowserFrm, SimpleExternalPumpBrowserFrm);Application.Run;// The form needs to be destroyed *BEFORE* stopping the scheduler.SimpleExternalPumpBrowserFrm.Free;GlobalCEFWorkScheduler.StopScheduler;end;FreeAndNil(GlobalCEFApp);FreeAndNil(GlobalCEFWorkScheduler);end.
而且每个pas文件里面也是有一些自己的特点的,所以要注意在使用的时候要保证格式的正确
即每个单元开始的位置加入了 {$I cef.inc},在本地程序编译的时候也要把这个文件加在pas文件所在的目录
unit Unit2;{$I cef.inc}interfaceuses{$IFDEF DELPHI16_UP}Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,{$ELSE}Windows, Messages, SysUtils, Variants, Classes, Graphics,Controls, Forms, Dialogs, StdCtrls, ExtCtrls,{$ENDIF}uCEFChromium, uCEFWindowParent, uCEFTypes, uCEFConstants, uCEFInterfaces, uCEFWorkScheduler,uCEFChromiumWindow;typeTSimpleExternalPumpBrowserFrm = class(TForm)AddressPnl: TPanel;GoBtn: TButton;Timer1: TTimer;URLCbx: TComboBox;ChromiumWindow1: TChromiumWindow;procedure GoBtnClick(Sender: TObject);procedure FormShow(Sender: TObject);procedure Timer1Timer(Sender: TObject);procedure FormCreate(Sender: TObject);procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);// These 3 TChromiumWindow events are called in the main app thread, so you can do whatever you want with the GUI.procedure ChromiumWindow1AfterCreated(Sender: TObject);procedure ChromiumWindow1BeforeClose(Sender: TObject);procedure ChromiumWindow1Close(Sender: TObject);procedure Chromium_OnBeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; var popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var noJavascriptAccess: Boolean; out Result: Boolean);protectedFCanClose : boolean;FClosing : boolean;procedure WMMove(var aMessage : TWMMove); message WM_MOVE;procedure WMMoving(var aMessage : TMessage); message WM_MOVING;procedure WMEnterMenuLoop(var aMessage: TMessage); message WM_ENTERMENULOOP;procedure WMExitMenuLoop(var aMessage: TMessage); message WM_EXITMENULOOP;end;varSimpleExternalPumpBrowserFrm : TSimpleExternalPumpBrowserFrm;GlobalCEFWorkScheduler : TCEFWorkScheduler = nil;procedure GlobalCEFApp_OnScheduleMessagePumpWork(const aDelayMS : int64);implementation{$R *.dfm}usesuCEFApplication;// This demo has a simple browser with a TChromiumWindow using the "External message pump" mode// to schedule the cef_do_message_loop_work calls thanks to the TCEFWorkScheduler class.// It was necessary to destroy the browser with the following destruction sequence :// 1. The FormCloseQuery event sets CanClose to False and calls TChromiumWindow.CloseBrowser, which triggers the TChromiumWindow.OnClose event.// 2. The TChromiumWindow.OnClose event calls TChromiumWindow.DestroyChildWindow which triggers the TChromiumWindow.OnBeforeClose event.// 3. TChromiumWindow.OnBeforeClose sets FCanClose to True and closes the form.procedure GlobalCEFApp_OnScheduleMessagePumpWork(const aDelayMS : int64);beginif (GlobalCEFWorkScheduler <> nil) then GlobalCEFWorkScheduler.ScheduleMessagePumpWork(aDelayMS);end;procedure TSimpleExternalPumpBrowserFrm.FormCreate(Sender: TObject);beginFCanClose := False;FClosing := False;end;procedure TSimpleExternalPumpBrowserFrm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);beginCanClose := FCanClose;if not(FClosing) thenbeginFClosing := True;Visible := False;AddressPnl.Enabled := False;ChromiumWindow1.CloseBrowser(True);end;end;procedure TSimpleExternalPumpBrowserFrm.FormShow(Sender: TObject);begin// For simplicity, this demo blocks all popup windows and new tabsChromiumWindow1.ChromiumBrowser.OnBeforePopup := Chromium_OnBeforePopup;// GlobalCEFApp.GlobalContextInitialized has to be TRUE before creating any browser// If it's not initialized yet, we use a simple timer to create the browser later.if not(ChromiumWindow1.CreateBrowser) then Timer1.Enabled := True;end;procedure TSimpleExternalPumpBrowserFrm.Chromium_OnBeforePopup(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const targetUrl, targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean; var popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo; var client: ICefClient; var settings: TCefBrowserSettings; var noJavascriptAccess: Boolean; out Result: Boolean);begin// For simplicity, this demo blocks all popup windows and new tabsResult := (targetDisposition in [WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW]);end;procedure TSimpleExternalPumpBrowserFrm.ChromiumWindow1AfterCreated(Sender: TObject);beginCaption := 'Simple External Pump Browser';AddressPnl.Enabled := True;GoBtn.Click;end;procedure TSimpleExternalPumpBrowserFrm.ChromiumWindow1BeforeClose(Sender: TObject);beginFCanClose := True;Close;end;procedure TSimpleExternalPumpBrowserFrm.ChromiumWindow1Close(Sender: TObject);begin// DestroyChildWindow will destroy the child window created by CEF at the top of the Z order.ChromiumWindow1.DestroyChildWindow;end;procedure TSimpleExternalPumpBrowserFrm.GoBtnClick(Sender: TObject);beginChromiumWindow1.LoadURL(URLCbx.Text);end;procedure TSimpleExternalPumpBrowserFrm.Timer1Timer(Sender: TObject);beginTimer1.Enabled := False;if not(ChromiumWindow1.CreateBrowser) and not(ChromiumWindow1.Initialized) thenTimer1.Enabled := True;end;procedure TSimpleExternalPumpBrowserFrm.WMMove(var aMessage : TWMMove);begininherited;if (ChromiumWindow1 <> nil) then ChromiumWindow1.NotifyMoveOrResizeStarted;end;procedure TSimpleExternalPumpBrowserFrm.WMMoving(var aMessage : TMessage);begininherited;if (ChromiumWindow1 <> nil) then ChromiumWindow1.NotifyMoveOrResizeStarted;end;procedure TSimpleExternalPumpBrowserFrm.WMEnterMenuLoop(var aMessage: TMessage);begininherited;if (aMessage.wParam = 0) and (GlobalCEFApp <> nil) then GlobalCEFApp.OsmodalLoop := True;end;procedure TSimpleExternalPumpBrowserFrm.WMExitMenuLoop(var aMessage: TMessage);begininherited;if (aMessage.wParam = 0) and (GlobalCEFApp <> nil) then GlobalCEFApp.OsmodalLoop := False;end;end.
将编译后的程序放到运行环境中
最终程序的运行情况如下:
浏览本地文件
from:点击查看