1

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.

  • 1
    Use Chrome. Open the same local HTML file, and then look at the location bar. What does the filename look like there? Does it match the format of yours? – Ken White Sep 09 '18 at 04:18
  • @KenWhite, is all correct (name of file and path). –  Sep 09 '18 at 11:41

2 Answers2

4

This is how I do it in my code:

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

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;
Jan Doggen
  • 8,799
  • 13
  • 70
  • 144