Compartilhar via


interface IWebView2WebView5

Note

This reference is no longer being maintained. For the latest API reference, see WebView2 API Reference.

interface IWebView2WebView5
  : public IWebView2WebView4

Additional functionality implemented by the primary WebView object.

Summary

Members Descriptions
add_ContainsFullScreenElementChanged Notifies when the ContainsFullScreenElement property changes.
remove_ContainsFullScreenElementChanged Remove an event handler previously added with the corresponding add_ event method.
get_ContainsFullScreenElement Indicates if the WebView contains a fullscreen HTML element.
add_WebResourceRequested Add an event handler for the WebResourceRequested event.
AddWebResourceRequestedFilter Adds a URI and resource context filter to the WebResourceRequested event.
RemoveWebResourceRequestedFilter Removes a matching WebResource filter that was previously added for the WebResourceRequested event.

You can QueryInterface for this interface from the object that implements IWebView2WebView. See the IWebView2WebView interface for more details.

Members

add_ContainsFullScreenElementChanged

Notifies when the ContainsFullScreenElement property changes.

public HRESULT add_ContainsFullScreenElementChanged(IWebView2ContainsFullScreenElementChangedEventHandler * eventHandler,EventRegistrationToken * token)

This means that an HTML element inside the WebView is entering fullscreen to the size of the WebView or leaving fullscreen. This event is useful when, for example, a video element requests to go fullscreen. The listener of ContainsFullScreenElementChanged can then resize the WebView in response.

    // Register a handler for the ContainsFullScreenChanged event.
    CHECK_FAILURE(m_webView->add_ContainsFullScreenElementChanged(
        Callback<IWebView2ContainsFullScreenElementChangedEventHandler>(
            [this](IWebView2WebView5* sender, IUnknown* args) -> HRESULT {
                if (m_fullScreenAllowed)
                {
                    CHECK_FAILURE(sender->get_ContainsFullScreenElement(&m_containsFullscreenElement));
                    if (m_containsFullscreenElement)
                    {
                        EnterFullScreen();
                    }
                    else
                    {
                        ExitFullScreen();
                    }
                }
                return S_OK;
            })
            .Get(),
        nullptr));

remove_ContainsFullScreenElementChanged

Remove an event handler previously added with the corresponding add_ event method.

public HRESULT remove_ContainsFullScreenElementChanged(EventRegistrationToken token)

get_ContainsFullScreenElement

Indicates if the WebView contains a fullscreen HTML element.

public HRESULT get_ContainsFullScreenElement(BOOL * containsFullScreenElement)

add_WebResourceRequested

Add an event handler for the WebResourceRequested event.

public HRESULT add_WebResourceRequested(IWebView2WebResourceRequestedEventHandler * eventHandler,EventRegistrationToken * token)

Fires when the WebView is performing an HTTP request to a matching URL and resource context filter that was added with AddWebResourceRequestedFilter. At least one filter must be added for the event to fire.

        if (m_blockImages)
        {
            m_webView->AddWebResourceRequestedFilter(L"*", WEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE);
            CHECK_FAILURE(m_webView->add_WebResourceRequested(
                Callback<IWebView2WebResourceRequestedEventHandler>(
                    [this](
                        IWebView2WebView* sender,
                        IWebView2WebResourceRequestedEventArgs* args) {
                        wil::com_ptr<IWebView2WebResourceRequestedEventArgs2>
                            webResourceEventArgs2;
                        args->QueryInterface(IID_PPV_ARGS(&webResourceEventArgs2));
                        WEBVIEW2_WEB_RESOURCE_CONTEXT resourceContext;
                        CHECK_FAILURE(
                            webResourceEventArgs2->get_ResourceContext(&resourceContext));
                        // Ensure that the type is image
                        if (resourceContext != WEBVIEW2_WEB_RESOURCE_CONTEXT_IMAGE)
                        {
                            return E_INVALIDARG;
                        }
                        // Override the response with an empty one to block the image.
                        // If put_Response is not called, the request will continue as normal.
                        wil::com_ptr<IWebView2WebResourceResponse> response;
                        CHECK_FAILURE(m_webViewEnvironment->CreateWebResourceResponse(
                            nullptr, 403 /*NoContent*/, L"Blocked", L"", &response));
                        CHECK_FAILURE(args->put_Response(response.get()));
                        return S_OK;
                    })
                    .Get(),
                &m_webResourceRequestedTokenForImageBlocking));
        }
        else
        {
            CHECK_FAILURE(m_webView->remove_WebResourceRequested(
                m_webResourceRequestedTokenForImageBlocking));
        }

AddWebResourceRequestedFilter

Adds a URI and resource context filter to the WebResourceRequested event.

public HRESULT AddWebResourceRequestedFilter(LPCWSTR const uri,WEBVIEW2_WEB_RESOURCE_CONTEXT const resourceContext)

URI parameter can be a wildcard string ('': zero or more, '?': exactly one). nullptr is equivalent to L"". See WEBVIEW2_WEB_RESOURCE_CONTEXT enum for description of resource context filters.

RemoveWebResourceRequestedFilter

Removes a matching WebResource filter that was previously added for the WebResourceRequested event.

public HRESULT RemoveWebResourceRequestedFilter(LPCWSTR const uri,WEBVIEW2_WEB_RESOURCE_CONTEXT const resourceContext)

If the same filter was added multiple times, then it will need to be removed as many times as it was added for the removal to be effective. Returns E_INVALIDARG for a filter that was never added.