0

Unless I'm missing something I think I've uncovered a bug with the TWebBrowser in Delphi.

I have a TPageControl with one TTabSheet on it and in that I have a TWebBrowser. The .SelectedEngine is set to EdgeIfAvailable.

I have the WebViewRuntime installed and I have the WebView2Loader.dll in my local directory. When the page has finished loading I can see the browser's .ActiveEngine is Edge and it works as expected.

However, when I try to create another TTabSheet at runtime and embed a TWebBrowser component inside that with the same .SelectedEngine set to EdgeIfAvailable it keeps on falling back to IE. When I look at Navigate() in SHDocVw.pas and debug the scenario I see that .GetEdgeInterface is nil and thus the browser acts as IE.

I don't understand why the design time component works fine but the runtime one doesn't. Here is my design time browser DFM:

object wbMain0: TWebBrowser
        Left = 0
        Top = 0
        Width = 1048
        Height = 356
        Align = alClient
        TabOrder = 0
        SelectedEngine = EdgeIfAvailable
        OnDocumentComplete = OnDocumentComplete
        ExplicitWidth = 740
        ExplicitHeight = 404
        ControlData = {
          4C000000506C0000CB2400000000000000000000000000000000000000000000
          000000004C000000000000000000000001000000E0D057007335CF11AE690800
          2B2E12620A000000000000004C0000000114020000000000C000000000000046
          8000000000000000000000000000000000000000000000000000000000000000
          00000000000000000100000000000000000000000000000000000000}
      end

When the user changes tabs I embed a new TWebBrowser control into the TTabSheet and navigate to the URL. Here is the TPageControl.ControlOnChange event:

procedure TfrmMainBrowser.pcMainChange(Sender: TObject);
begin
  if fbBusy then
    Exit;

  if pcMain.ActivePageIndex > 0 then
  begin

    if pcMain.Pages[pcMain.ActivePageIndex].ComponentCount = 0 then
    begin

      var loBrowser := TWebBrowser.Create(pcMain.Pages[pcMain.ActivePageIndex]);
      loBrowser.SetParentComponent(pcMain.Pages[pcMain.ActivePageIndex]);
      loBrowser.Silent := True;
      loBrowser.SelectedEngine := EdgeIfAvailable;
      loBrowser.Align := alClient;
      loBrowser.OnDocumentComplete := OnDocumentComplete;
      loBrowser.Show;

      //Can't set via TWebBrowser, is this necessary?
      //loBrowser.UserDataFolder := TPath.Combine(TPath.GetDirectoryName(Application.ExeName), 'CustomCache');
      loBrowser.Navigate(pcMain.ActivePage.Caption);

    end;
  end;
end;

As a test I change the runtime creation to EdgeOnly and that still falls back to IE. Which is odd because I expect a blank page. But instead it still uses IE.

This is from the documentation:

TWebBrowser.TSelectedEngine.EdgeOnly: the TWebBrowser attempts to use the Edge WebView2 browser control, however if this is not possible then no browsing is possible as there is no fallback option.

If I change the dynamically created TWebBrowser component to a TEdgeBrowser it also works correctly:

 var loBrowser := TEdgeBrowser.Create(pcMain.Pages[pcMain.ActivePageIndex]);
  loBrowser.SetParentComponent(pcMain.Pages[pcMain.ActivePageIndex]);
  loBrowser.Align := alClient;
  loBrowser.Show;
  loBrowser.Navigate(pcMain.ActivePage.Caption);

What am I missing?

AmigoJack
  • 5,234
  • 1
  • 15
  • 31
Frank Pedro
  • 198
  • 2
  • 10
  • I've decided to imbed a TEdgeBrowser instead since that is working fine. Think I'll open a bug log at Embarcaderro. – Frank Pedro Dec 01 '22 at 07:35
  • I already have an Embarcadero listing [https://quality.embarcadero.com/browse/RSP-36092](https://quality.embarcadero.com/browse/RSP-36092). According to Macro Cantù it would be a problem of WebView2 and therefore of Microsoft. – USauter Dec 07 '22 at 10:34
  • 1
    According to your ticket, I also discovered the control is not visible it behaves wonky. – Frank Pedro Dec 07 '22 at 12:11

0 Answers0