Home > Mobile >  TEdgeBrowser - OnNewWindowRequested - Open New Window in another TEdgeBrowser
TEdgeBrowser - OnNewWindowRequested - Open New Window in another TEdgeBrowser

Time:12-22

Delphi 10.4.2

I'm working with TEdgeBrowser for the first time and am playing with some ideas in a small demo app I created. I have so far been able to programmatically Navigate to the website, login (fill in the User Name and Password and click the Log In button), click a button (My Cases) to be taken to another page, then filled in an edit field and a combo box to lookup a specific case. So far all is well. Now that the case is available I have the option to click either "History" or "Service information". I clicked on the "History" button and want the new window to open in another TEdgeBrowser component. Here is the code I'm using to attempt to do this but it is not working.

procedure TfrmMain.EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser;
  Args: TNewWindowRequestedEventArgs);
begin
  //* Natigate to the history page
  pgeMain.ActivePage := tabHistory;

  //* Open link in another WebView2 (new tab):
  Args.ArgsInterface.Set_NewWindow(EdgeBrowserHistory.DefaultInterface);
end;

Please note that I have the following in the FormShow event.

procedure TfrmMain.FormShow(Sender: TObject);
begin
  EdgeBrowser.UserDataFolder := 'C:\Temp\';

  EdgeBrowserHistory.CreateWebView;
end;

Note: My main browser is call EdgeBrowser

procedure TfrmMain.btnGoClick(Sender: TObject);
begin
  pgeMain.ActivePage := tabBrowser;
  EdgeBrowser.Navigate(edtWebSiteURL.Text);
end;

Reading other post on this site, I thought maybe it had to do with having a TEdgeBrowser on another page of a TPageControl, so I moved it off to the main form and it still does not work.

If I do not have any code in the EdgeBrowserNewWindowRequested event it will open to a new window just fine. Anyone know what I need to do to get it to work?

CodePudding user response:

You just need to navigate the second TEdgeBrowser to the URL you want it to go to. (Also, note that when you use the Args interface to get a PWideChar, you need a matching call to CoTaskMemFree). And, assuming you may have other "tabs" or any other buttons/links that could trigger NewWindowRequest, you might need some logic to handle those other cases as the code below will go to the History tab for any NewWindowRequest. I'm not sure if you need to set EdgeBrowserHistory.UserDataFolder as well or if it will default to the same as the first.

Try something like this:

procedure TfrmMain.EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser; Args: TNewWindowRequestedEventArgs);
var
  PUri: PWideChar;
  Url: string;
begin
  //* Navigate to the history page
  pgeMain.ActivePage := tabHistory;

  //* Open link in another WebView2 (new tab):
  Args.ArgsInterface.Get_uri(PUri);
  Url := PUri;
  CoTaskMemFree(PUri);
  EdgeBrowserHistory.Navigate(Url);
  Args.ArgsInterface.Set_Handled(1);
end;

CodePudding user response:

Greeting Greg,

Thank you for your reply. I did try to do as you suggested yesterday while trying to do anything I could think of to make this work, but, because I have to log into this site, it does not like trying to access this page using Navigate().

Here you can see the main TEdgeBrowser on the left and the second one on the right. And yes, this time it did populate it with something at least (A Warning Message). Any other ideas on getting this to work in a another EdgeBrowser?

enter image description here

If we cannot get this to work in another EdgeBrowser which is the preferred way, I'm ok with the page displaying in the current EdgeBrowser using the following code:

procedure TfrmMain.EdgeBrowserNewWindowRequested(Sender: TCustomEdgeBrowser;
  Args: TNewWindowRequestedEventArgs);
begin
  //* Open link in current WebView2:
  Args.ArgsInterface.Set_NewWindow(Sender.DefaultInterface);
end;

Although it works to open "History" in current EdgeBrower, there is now another issue, the back button is disabled, and if the back button is disabled, I cannot go back to the previous page and click the "Service Information" button.

If I use MS Edge to go to this website, lookup the case and click the "History" button, the website opens the page in a new tab and if I go back to the main tab of course the back button is disabled there, so I do not see anyway around this issue, unless you know of one.

So that leaves only one option, which I really don't care for much and that is letting EdgeBrower do it's own thing and open the History in a new window. This presents it own set of issues.

  1. Can the size of the new window be controlled?
  2. Can the placement of the new window be controlled?
  3. If I close my form, the two new windows (History and Service information) remain open. This is not good.

So bottom line, if this is my only option, I really need to be able to have control over those windows. Any ideas if that is possible?

Thanks again for any assistance you can provide.

  • Related