Page 1 of 1

How do I send a webpage to another browser window on the same form?

Posted: Wed Dec 22, 2021 2:37 pm
by miket@cbofks.com
Greetings Salvadordf,

When I click on a button on a webpage I want the page not to open in a new window (which is the default) but rather open in another view (TWVWindowParent) on the same form. Is this possible?

I know I need to use the OnNewWindowRequested() event but not sure what to place in the parameters

Code: Select all

procedure TfrmMain.WVBrowserNewWindowRequested(Sender: TObject;
  const aWebView: ICoreWebView2;
  const aArgs: ICoreWebView2NewWindowRequestedEventArgs);
begin
  aArgs.Set_NewWindow(???);
end;
Do I need to also need sArgs.Set_Handled(1); to tell WebView2 that I have already processed the request to open a new window?

Thanks for developing this new component set. Looking forward to working with it.

Mike

Re: How do I send a webpage to another browser window on the same form?

Posted: Wed Dec 22, 2021 4:36 pm
by salvadordf
Hi,

Yes, it's possible.

The PopupBrowser demo shows how to open a browser in a child form and you can use most of that code.

Follow these steps :
  • Use the TWVBrowser.OnNewWindowRequested event and create a TCoreWebView2NewWindowRequestedEventArgs instance with the "aArgs" parameter. Call it "MyArgs".
  • Create a TCoreWebView2Deferral instance with "MyArgs.Deferral" in the Create constructor. Call it "MyDeferral".
  • Call TWVBrowser.CreateBrowser for the new browser.
  • Use the TWVBrowser.OnAfterCreated event to set MyArgs.NewWindow and MyArgs.Handled. Also call MyDeferral.Complete.
  • Destroy MyArgs and MyDeferral.

Re: How do I send a webpage to another browser window on the same form?

Posted: Wed Dec 22, 2021 6:19 pm
by miket@cbofks.com
I got it working perfectly, thanks for your clear instructions.

Here is what I ended up doing for anyone that is interested.

I dropped another WVBrowser and TWVWindowParent components on the same form.

Code: Select all

  private
    { Private declarations }
    //* Send to another web browser on the same form
    FArgs: TCoreWebView2NewWindowRequestedEventArgs;
    FDeferral: TCoreWebView2Deferral;

Code: Select all

procedure TfrmMain.WVBrowser2AfterCreated(Sender: TObject);
begin
  if assigned(FArgs) and assigned(FDeferral) then
    try
      FArgs.NewWindow := WVBrowser2.CoreWebView2.BaseIntf;
      FArgs.Handled   := True;

      FDeferral.Complete;
    finally
      FreeAndNil(FDeferral);
      FreeAndNil(FArgs);
    end;

  WVWindowParent2.UpdateSize;
end;

Code: Select all

procedure TfrmMain.WVBrowserNewWindowRequested(Sender: TObject;
  const aWebView: ICoreWebView2;
  const aArgs: ICoreWebView2NewWindowRequestedEventArgs);
begin
  FArgs := TCoreWebView2NewWindowRequestedEventArgs.Create(aArgs);
  FDeferral := TCoreWebView2Deferral.Create(FArgs.Deferral);
  WVBrowser2.CreateBrowser(WVWindowParent2.Handle);
end;

Re: How do I send a webpage to another browser window on the same form?

Posted: Sat Mar 19, 2022 7:42 pm
by pushca
miket@cbofks.com wrote: Wed Dec 22, 2021 6:19 pm I got it working perfectly, thanks for your clear instructions.

Here is what I ended up doing for anyone that is interested.

I dropped another WVBrowser and TWVWindowParent components on the same form.

Code: Select all

  private
    { Private declarations }
    //* Send to another web browser on the same form
    FArgs: TCoreWebView2NewWindowRequestedEventArgs;
    FDeferral: TCoreWebView2Deferral;

Code: Select all

procedure TfrmMain.WVBrowser2AfterCreated(Sender: TObject);
begin
  if assigned(FArgs) and assigned(FDeferral) then
    try
      FArgs.NewWindow := WVBrowser2.CoreWebView2.BaseIntf;
      FArgs.Handled   := True;

      FDeferral.Complete;
    finally
      FreeAndNil(FDeferral);
      FreeAndNil(FArgs);
    end;

  WVWindowParent2.UpdateSize;
end;

Code: Select all

procedure TfrmMain.WVBrowserNewWindowRequested(Sender: TObject;
  const aWebView: ICoreWebView2;
  const aArgs: ICoreWebView2NewWindowRequestedEventArgs);
begin
  FArgs := TCoreWebView2NewWindowRequestedEventArgs.Create(aArgs);
  FDeferral := TCoreWebView2Deferral.Create(FArgs.Deferral);
  WVBrowser2.CreateBrowser(WVWindowParent2.Handle);
end;
Thank you so much for the example! I use it, but I have a problem that this action can be done only once. Nothing happens on the second attempt.

Re: How do I send a webpage to another browser window on the same form?

Posted: Sun Mar 20, 2022 11:06 am
by salvadordf
Your code seems to be fine. Compare the rest of the code to the TabbedBrowser demo.

TabbedBrowser can handle multiple browsers in the same form and it works correctly when you open the same page in a new tab multiple times.