user9672569
user9672569

Reputation:

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

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.

Upvotes: 1

Views: 6450

Answers (2)

Jan Doggen
Jan Doggen

Reputation: 9096

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;

Upvotes: 0

complete_stranger
complete_stranger

Reputation: 908

This is how I do it in my code:

CBrowser.Load('file:///' + ReplaceStr(fpath, '\', '/'));

Upvotes: 4

Related Questions