Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding NewWindowEvent handler to my Win32 class (not a bug) #4187

Closed
ajtruckle opened this issue Nov 22, 2023 · 2 comments
Closed

Adding NewWindowEvent handler to my Win32 class (not a bug) #4187

ajtruckle opened this issue Nov 22, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@ajtruckle
Copy link

ajtruckle commented Nov 22, 2023

What happened?

My webbrowser control class is based on this tutorial https://mariusbancila.ro/blog/2020/01/29/using-microsoft-edge-in-a-native-windows-desktop-app-part-2/ and not the official samples.

I show that code in a Stack Overflow answer: https://stackoverflow.com/a/68183783/2287576

I only have one viewer on my dialog and I want to intercept new window request so that if it is a certain link it is suppressed and spawned in the default system browser.

But I don't understand how to interface the official sample code in the browser class that I am using.

Thank you for your help.

Importance

Moderate. My app's user experience is affected, but still usable.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

No response

SDK Version

No response

Framework

Win32

Operating System

Windows 11

OS Version

No response

Repro steps

Display html in your viewer that has a hyperlink using my class (on a dialog). Need to detect that link so I can disoplay it in system browser and not webview2.

Repros in Edge Browser

Not Applicable

Regression

No.

Last working version (if regression)

No response

@ajtruckle ajtruckle added the bug Something isn't working label Nov 22, 2023
@ajtruckle
Copy link
Author

Reference: #2587

@ajtruckle
Copy link
Author

I seem to have this working the way I want it now:

	// Register a handler for the NewWindowRequested event.
	CHECK_FAILURE(m_pImpl->m_webView->add_NewWindowRequested(
		Callback<ICoreWebView2NewWindowRequestedEventHandler>(
			[this](ICoreWebView2* sender, ICoreWebView2NewWindowRequestedEventArgs* args) {

				// get the target Uri
				LPWSTR sUri;
				CHECK_FAILURE(args->get_Uri(&sUri));

				// and if it was user initiated (e.g. click on an anchor tag)
				BOOL bUserInit;
				CHECK_FAILURE(args->get_IsUserInitiated(&bUserInit));

				if (bUserInit && wcsncmp(sUri, L"file:", 5) != 0) {
					// cancel default behaviour
					args->put_Handled(TRUE);

					// and open Uri in default browser
					ShellExecute(0, 0, sUri, 0, 0, SW_SHOW);
				}

				return S_OK;
			})
		.Get(), nullptr));

It works as I want it to work. So file prefixed URLs are handled by WebView2 and others by a new browser instance. As long as my hyperlinks have a target property ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant