How load a local html file with Chromium Embedded (CEF)?

5.9k Views Asked by At

I want load a html file into Chromium (CEF4Delphi) but nothing is showed, only a white page.

Is possible load a local html file using the following approach?

Here is html file.

Also have other trouble that is everytime that Chromium is executed, also is executed other instance of my application. How solve this?

Code used:

var
  Form1: TForm1;
  FStarted: Boolean;

implementation

{$R *.dfm}

function CEFApplication: TCefApplication;
var
  sPath: String;
begin
  sPath := ExtractFilePath(ParamStr(0));
  if not assigned(GlobalCEFApp) then
  begin
    GlobalCEFApp := TCefApplication.Create;
    GlobalCEFApp.FlashEnabled := False;
    GlobalCEFApp.FastUnload := True;

    GlobalCEFApp.FrameworkDirPath := sPath + 'cef';
    GlobalCEFApp.ResourcesDirPath := sPath + 'cef';
    GlobalCEFApp.LocalesDirPath := sPath + 'cef\locales';
    GlobalCEFApp.Cache := sPath + 'cef\cache';
    GlobalCEFApp.Cookies := sPath + 'cef\cookies';
    GlobalCEFApp.UserDataPath := sPath + 'cef\User Data';
    GlobalCEFApp.EnableGPU := False;
  end;
  if not FStarted then
    FStarted := GlobalCEFApp.StartMainProcess;

  result := GlobalCEFApp;
end;

initialization

CEFApplication;

end.

Form2:

procedure TForm2.FormShow(Sender: TObject);
begin
  while not(Chromium1.CreateBrowser(CEFWindowParent1, '')) and
    (Chromium1.Initialized) do
  begin
    Sleep(100);
    Application.processMessages;
  end;
  Chromium1.LoadURL(ExtractFilePath(ExtractFilePath(Application.ExeName)) + 'gmaps.html');
end;

EDITION:

Relative to my doubt about multiple instance of my application being executed, this is normal and right based on this article.

2

There are 2 best solutions below

0
complete_stranger On BEST ANSWER

This is how I do it in my code:

CBrowser.Load('file:///' + ReplaceStr(fpath, '\', '/'));
0
Jan Doggen On

CEF4Delphi has a TChromium.LoadString for that.

I do it in a protected

procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED;

like this:

procedure TDialoogDeclaratieGoogleMaps.BrowserCreatedMsg(var aMessage : TMessage);
begin
  PanelBrowser.UpdateSize;      // The TCEFWindowParent
  ChromiumBrowser.LoadString(FGoogleHTML);   // String read from file earlier
end;

and that message gets posted in the afterCreated method:

procedure TDialoogDeclaratieGoogleMaps.ChromiumBrowserAfterCreated(Sender: TObject; const browser: ICefBrowser);
begin
  PostMessage(Handle, CEF_AFTERCREATED, 0, 0);
end;