Replies: 6 comments
-
Hey @ic2ic2 - The "enlarged" issue is probably related to DPI. If you're using Winforms this may help: https://docs.microsoft.com/en-us/dotnet/desktop/winforms/high-dpi-support-in-windows-forms?view=netframeworkdesktop-4.8#configuring-your-windows-forms-app-for-high-dpi-support Where a link opens (same tab, new tab, new window) often depends on the type of link, as well as how it was clicked. By default, a ctrl+click results in a new tab (which in WebView2 is a NewWindowRequested). Are you looking for:
If the latter, then you will likely need to do your click handling code on the webpage (by injecting script), and then telling the host app to open a link in the default browser through a web message or host object. Do you want to give that a try? Let me know if you need more details on that approach. |
Beta Was this translation helpful? Give feedback.
-
Hello Champnic, Thanks for the reply! The DPI issue should not apply as I call WebView2 in a WPF window, not Winforms. But for me that's a minor issue. Regarding what I am looking for is: 2 Yes, but not OR...but AND (with the code I have in my first message). So 1) a URL click should perform the Process.Start and not change the WebView2 content, and 2) a Ctrl click on a URL should change the content to the URL, in the same window (in our case we also open the URL in the default browser) I have 2 problems: 1 I can not detect the Ctrl in the CoreWebView2_NewWindowRequested. So yes, please, more details how to do that with injecting a script would help a lot (I now use protected override void OnKeyDownbut that sets the Ctrl key independent of the mouse click, so not always correctly) 2 I found that if target="_blank" is not present in a <a href="..."> URL HTML statement, the CoreWebView2_NewWindowRequested is not called at all with a click on the URL (but it with a Ctrl click)! Should I report that as a bug or is it by design? In our case we can handle it because we pass the HTML of an e-mail to the WebView2 based program and we can change hyperlinks html if necessary. But I think this method should be called in all URL clicks? I am looking forward to your reply, Dick |
Beta Was this translation helpful? Give feedback.
-
One more comment: the above CoreWebView2_NewWindowRequested seems to work rather erratic. I've seen it opening a new Webview2 window despite the code not allowing that. Opening the same e-mail it depends from where I opened the WPF window with the Weview2 on it if a plain click on a URL ( e.Handled = true) does not open a window or does open a window, but only the first time I click on a URL. Or I apply my temporary method to simulate Ctrl mouseclick, this opens the URL within the WebView2 window as programmed but the next plain then click sudden opens a new WebView2 window which has not been programmed at all..... |
Beta Was this translation helpful? Give feedback.
-
Ok I think I'm starting to understand more. The behavior you are describing doesn't sound like something the NewWindowRequested event is directly intended to solve. That event is supposed to fire whenever the runtime would open a new tab or window. But it sounds from your description that you also want to handle regular clicks, or otherwise decide whether something should be opening in a new window or not. If I'm correct, you are looking for something closer to a "LinkClicked" event, right? And then within that event having enough info to decide whether it should be a regular navigation, or new window, or default browser, etc. Does that sound about right? I think the easiest way to accomplish this today might be to inject javascript that listens for link clicks on the page, and then send a webmessage up to your host app with whatever info you need. |
Beta Was this translation helpful? Give feedback.
-
Hello Champnic, Thanks, Dick |
Beta Was this translation helpful? Give feedback.
-
Use ExcecuteScriptAsync to inject something like this:
|
Beta Was this translation helpful? Give feedback.
-
I start this as a question which, depending on the answer, could become a feature request.
I can't seem to detect if the user just left clicked the URL, or with e.g. Ctrl or Shift. I see no way to detect that in the CoreWebView2_NewWindowRequested and something like this.PreviewMouseLeftButtonDown +=HandleMouse is called in all controls except in the WebView2 control. With the code below I noticed that Ctrl or Shift click opens a URL in the same window (but enlarged) while a plain left click opens a new WebView2 runtime. With e.Handled=true a plain click opens a new (enlarged) runtime with the URL's content, Ctrl or Shift Click opens in the same runtime.
I want the Ctrl Click on a URL to open in the default browser (using Process.Start(cURL) and a click on a URL to remain in the current WebView2 runtime) window without resizing it.
Is there a at least a way to find out if Ctrl or Shift Click or a plain click was done in the code below?
Beta Was this translation helpful? Give feedback.
All reactions