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.

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

Post Reply
miket@cbofks.com
Posts: 21
Joined: Tue Aug 03, 2021 7:23 pm

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

Post 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
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

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

Post 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.
miket@cbofks.com
Posts: 21
Joined: Tue Aug 03, 2021 7:23 pm

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

Post 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;
pushca
Posts: 37
Joined: Sat Dec 04, 2021 5:09 pm

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

Post 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.
User avatar
salvadordf
Posts: 4620
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

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

Post 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.
Post Reply