Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.
I had different CEF windows in my application.
At one point when a form gets closed I want to close that specific CEF instance.
I looked at the documentation but it's different in different examples.
procedure TForm_ChromeRepdoc.Chromium1Close(Sender: TObject; const browser: ICefBrowser; var aAction : TCefCloseBrowserAction);
begin
if (browser <> nil) and
(ChromiumWindow1.ChromiumBrowser.BrowserId = browser.Identifier) and
(ChromiumWindow1 <> nil) then
begin
PostMessage(Handle, CEFBROWSER_DESTROY, 0, 0);
aAction := cbaDelay;
end;
end;
procedure TForm_ChromeRepdoc.Chromium1BeforeClose(Sender: TObject; const browser: ICefBrowser);
begin
// The main browser is being destroyed
if (ChromiumWindow1.ChromiumBrowser.BrowserId = 0) then
begin
FCanClose := True;
PostMessage(Handle, WM_CLOSE, 0, 0);
end;
end;
procedure TForm_ChromeRepdoc.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
try
if BestellId <> TGUID.Empty then
begin
try
TThread.Queue(nil,
procedure
begin
DatensatzVeraendertBestellung();
end);
except on E: Exception do
end;
end;
finally
BestellId := TGuid.Empty;
end;
CanClose := FCanClose;
if not(FClosing) then
begin
FClosing := True;
Visible := False;
ChromiumWindow1.ChromiumBrowser.CloseAllBrowsers();
end;
end;
But Chromium1Close gets called but Chromium1BeforeClose never.
TChromiumWindow is only intended for extremely simple browsers.
If your application needs to show several child forms with a browser on each of them I would recommend that you use the code in the ToolBoxBrowser demo.
ToolBoxBrowser has some extra code that closes the browser if the user presses the ESC key and the form borders are fixed but you can change all that easily.
Read the code comments in uMainForm.pas and uChildForm.pas
Each child form follows the usual destruction steps from the simpleBrowser2 demo and they also send a CEFBROWSER_CHILDDESTROYED message to the main form to check if the main form should also be closed in case there are no other child form alive.
The previous code is using a TChromiumWindow component with the destruction sequence of the MiniBrowser demo, which uses TChromium and TCEFWindowParent and it has TChromium.MultiBrowserMode set to True.
That destruction sequence can't be used with the original TChromiumWindow component and this is the reason the browser is not fully destroyed, causing the shutdown problems.
I haven't tried to enable MultiBrowserMode in a TChromiumWindow browser but I guess it would reqiere many code changes to that component.
If you use MultiBrowserMode then consider copying the code from the MiniBrowser demo and replacing TChromiumWindow with a TChromium and a TCEFWindowParent component.
If you just need to open child forms with independent browsers then use the ToolBoxBrowser demo I mentioned.