0

I have a winforms app with an embedded WebView2 browser. When I click on a link that has a target, it opens a new window with a WebView2 Icon in the taskbar. The window only shows the page and the address bar, which is not editable.

I believe this window can also be created by calling the e.NewWindow or Window.Open() method.

I know that I can use the NewWindowRequested event to get the parameters for this window, but how do I set parameters, like the width, height or position?

I have only searched for answers.

DaveRus
  • 1
  • 1
  • [e.NewWindow = (CoreWebView2)sender still results in a separate instance](https://stackoverflow.com/a/68790332/7444103) – Jimi Jan 01 '23 at 14:22
  • The eventargs of the event, has a [WindowFeatures](https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2windowfeatures?view=webview2-dotnet-1.0.1462.37&WT.mc_id=DT-MVP-5003235) property that helps you to find those information about the window size. – Reza Aghaei Jan 01 '23 at 14:24
  • Thanks, Jimi, I can creaate the popup no problem, I just can't set the size of it. Reza, that lets me get the window size but I want to set the window size – DaveRus Jan 01 '23 at 21:52
  • The Popup in that code is YOUR Window, you can set whatever size you want, as you do with whatever other Form – Jimi Jan 01 '23 at 22:31
  • 'I want to set window size' → If 'window' refers to the original popup window, then it's the script which sets it, for [example](https://www.w3schools.com/jsref/met_win_open.asp) in the html code of the page, there's a script like this `window.open("https://www.w3schools.com", "_blank", "toolbar=yes,scrollbars=yes,resizable=yes,top=500,left=500,width=400,height=400");`. – Reza Aghaei Jan 01 '23 at 23:27
  • Then in your C# code, in the `NewWindowRequested` event handler, you can get those location and size information from `e.WindowFeatures`, and use those information to set location and size of your custom popup, using its Size and Location properties. – Reza Aghaei Jan 01 '23 at 23:29
  • The contents of the main webview is just a page with links on it. When a link, with a target=_blank, on that page is clicked the popup window is created automatically, it is not generated by window.open. Webview is generating it itself. However, as I said, the same window can be generated by window.open, and yes, I can control the parameters to modify it's size when it opens that way. The problem is that it is being generated by clicking a link and I can't find out how to pass parameters to that window when it pops up that way. – DaveRus Jan 02 '23 at 09:49
  • When there's no Width and Height defined there, then you should use the default size and location of the popup form. Or maybe I do not see the problem or didn't get the requirement :/ – Reza Aghaei Jan 02 '23 at 13:04
  • Thanks for responding, maybe I'm not explaining it well. If I right click a link from a website in a webview window and choose 'open in new window', a seperate window appears with only an un editable address bar. I want to control the size of this window. – DaveRus Jan 02 '23 at 22:33
  • As mentioned in above comments, the pop up form is your form, and you can control it's size like any other form, like the main form of your application. It's not quite clear what window you are referring or what the expectation, and why the comments doesn't answer your question. Please edit the question, and add more details, including the HTML or the URL which shows an example of the content. Also tell what's the exact expectation when you click on the link. – Reza Aghaei Jan 04 '23 at 16:45
  • "a separate window appears with only an un editable address bar" - if you see that it means that you aren't setting the NewWindowRequestedEventArgs.NewWindow property. You use this property to say which CoreWebView2 to use as the new window. You can create a new CoreWebView2 for this property and put the new CoreWebView2 in a window you control and set its size and position. If you don't set the NewWindow property, WebView2 opens its own popup window that you cannot control. See https://stackoverflow.com/a/68790332/7444103 – David Risney Jan 04 '23 at 18:54

1 Answers1

0

I used the advice given and this works for me now. I had no control over the default window that was being generated by WebView, so created a new form with a WebView2 control and used the following code to handle it.

webView2Dashboard.CoreWebView2.NewWindowRequested += OnNewWindowRequested;

private void OnNewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
  {
    e.Handled = true;
    var url = e.Uri;           
    var browser = new WebViewBrowser(url);
    browser.Show();
  }
DaveRus
  • 1
  • 1