共用方式為


interface ICoreWebView2CompositionController4

Note

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

interface ICoreWebView2CompositionController4
  : public ICoreWebView2CompositionController3

This Interface includes an API which enables non-client hit-testing support for WebView2.

Summary

Members Descriptions
add_NonClientRegionChanged This method is used to add a listener for NonClientRegionChanged.
GetNonClientRegionAtPoint If you are hosting a WebView2 using CoreWebView2CompositionController, you can call this method in your Win32 WndProc to determine if the mouse is moving over or clicking on WebView2 web content that should be considered part of a non-client region.
QueryNonClientRegion This method is used to get the collection of rects that correspond to a particular COREWEBVIEW2_NON_CLIENT_REGION_KIND.
remove_NonClientRegionChanged This method is used to remove an event handler previously added with add_NonClientRegionChanged.

Applies to

Product Introduced
WebView2 Win32 1.0.2420.47
WebView2 Win32 Prerelease 1.0.2415

Members

add_NonClientRegionChanged

This method is used to add a listener for NonClientRegionChanged.

public HRESULT add_NonClientRegionChanged(ICoreWebView2NonClientRegionChangedEventHandler * eventHandler, EventRegistrationToken * token)

The event is fired when regions which are marked as non-client in the app html have changed. So either when new regions have been marked, or unmarked, or the region(s) have been changed to a different kind.

void ScenarioNonClientRegionSupport::AddChangeListener()
{
    if (m_compController4)
    {
        CHECK_FAILURE(m_compController4->add_NonClientRegionChanged(
            Callback<ICoreWebView2NonClientRegionChangedEventHandler>(
                [this](
                    ICoreWebView2CompositionController* sender,
                    ICoreWebView2NonClientRegionChangedEventArgs* args) -> HRESULT
                {
                    COREWEBVIEW2_NON_CLIENT_REGION_KIND region =
                        COREWEBVIEW2_NON_CLIENT_REGION_KIND_NOWHERE;
                    args->get_RegionKind(&region);
                    wil::com_ptr<ICoreWebView2RegionRectCollectionView> regionsCollection;
                    m_compController4->QueryNonClientRegion(region, &regionsCollection);
                    UINT32 count = 0;
                    regionsCollection->get_Count(&count);
                    RECT rect;
                    regionsCollection->GetValueAtIndex(0, &rect);
                    return S_OK;
                })
                .Get(),
            &m_nonClientRegionChanged));
    }
}

GetNonClientRegionAtPoint

If you are hosting a WebView2 using CoreWebView2CompositionController, you can call this method in your Win32 WndProc to determine if the mouse is moving over or clicking on WebView2 web content that should be considered part of a non-client region.

public HRESULT GetNonClientRegionAtPoint(POINT point, COREWEBVIEW2_NON_CLIENT_REGION_KIND * value)

The point parameter is expected to be in the client coordinate space of WebView2. The method sets the out parameter value as follows:

  • COREWEBVIEW2_NON_CLIENT_REGION_KIND_CAPTION when point corresponds to a region (HTML element) within the WebView2 with -webkit-app-region: drag CSS style set.

  • COREWEBVIEW2_NON_CLIENT_REGION_KIND_CLIENT when point corresponds to a region (HTML element) within the WebView2 without -webkit-app-region: drag CSS style set.

  • COREWEBVIEW2_NON_CLIENT_REGION_KIND_NOWHERE when point is not within the WebView2.

NOTE: in order for WebView2 to properly handle the title bar system menu, the app needs to send WM_NCRBUTTONDOWN and WM_NCRBUTTONUP to SendMouseInput. See sample code below.

            || message == WM_NCRBUTTONDOWN || message == WM_NCRBUTTONUP
    if (message == WM_NCHITTEST && compositionController4)
    {
        POINT point{GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
        ScreenToClient(hWnd, &point);
        if (PtInRect(&m_webViewBounds, point))
        {
            point.x -= m_webViewBounds.left;
            point.y -= m_webViewBounds.top;

            COREWEBVIEW2_NON_CLIENT_REGION_KIND region =
                COREWEBVIEW2_NON_CLIENT_REGION_KIND_NOWHERE;
            CHECK_FAILURE(compositionController4->GetNonClientRegionAtPoint(point, &region));
            *result = region;
            return true;
        }
    }

QueryNonClientRegion

This method is used to get the collection of rects that correspond to a particular COREWEBVIEW2_NON_CLIENT_REGION_KIND.

public HRESULT QueryNonClientRegion(COREWEBVIEW2_NON_CLIENT_REGION_KIND kind, ICoreWebView2RegionRectCollectionView ** rects)

This is to be used in the callback of add_NonClientRegionChanged whose event args object contains a region property of type COREWEBVIEW2_NON_CLIENT_REGION_KIND.

void ScenarioNonClientRegionSupport::AddChangeListener()
{
    if (m_compController4)
    {
        CHECK_FAILURE(m_compController4->add_NonClientRegionChanged(
            Callback<ICoreWebView2NonClientRegionChangedEventHandler>(
                [this](
                    ICoreWebView2CompositionController* sender,
                    ICoreWebView2NonClientRegionChangedEventArgs* args) -> HRESULT
                {
                    COREWEBVIEW2_NON_CLIENT_REGION_KIND region =
                        COREWEBVIEW2_NON_CLIENT_REGION_KIND_NOWHERE;
                    args->get_RegionKind(&region);
                    wil::com_ptr<ICoreWebView2RegionRectCollectionView> regionsCollection;
                    m_compController4->QueryNonClientRegion(region, &regionsCollection);
                    UINT32 count = 0;
                    regionsCollection->get_Count(&count);
                    RECT rect;
                    regionsCollection->GetValueAtIndex(0, &rect);
                    return S_OK;
                })
                .Get(),
            &m_nonClientRegionChanged));
    }
}

remove_NonClientRegionChanged

This method is used to remove an event handler previously added with add_NonClientRegionChanged.

public HRESULT remove_NonClientRegionChanged(EventRegistrationToken token)