如何在MFC或C ++中实例化IWebViewControl?

时间:2018-08-27 10:20:03

标签: c++ mfc windows-runtime

最近,Microsoft为WPF和Winforms引入了IWebViewControlIWebViewControl使用Edge渲染引擎。 使用类构造函数实例化IWebViewControl非常容易,但是我不确定如何在MFC或C ++中实例化IWebViewControl

https://docs.microsoft.com/en-us/microsoft-edge/hosting/webview

2 个答案:

答案 0 :(得分:0)

我不知道这是否是答案,将根据要求将其删除。但是,微软现在有一个Beta版的WebView2,并且具有Win32变体:

https://docs.microsoft.com/en-us/microsoft-edge/webview2/gettingstarted/win32

由于MFC是Win32 / Win64应用程序,因此无法使用此SDK吗?

答案 1 :(得分:-1)

这是C ++中的IWebViewControl示例: https://github.com/rjmurillo/webview-samples/tree/master/WinMD/Win32/WebViewSamples.Win32

摘录自代码:

void CheckFailure(_In_ HRESULT hr)
{
    if (FAILED(hr))
    {
        WCHAR message[512] = L"";
        StringCchPrintf(message, ARRAYSIZE(message), L"Error: 0x%x", hr);
        MessageBoxW(nullptr, message, nullptr, MB_OK);
        ExitProcess(-1);
    }
}

template <typename TInterface>
Microsoft::WRL::ComPtr<TInterface> GetActivationFactoryFailFast(_In_z_ PCWSTR factoryClassName)
{
    ComPtr<TInterface> factoryInstance;
    CheckFailure(RoGetActivationFactory(
        HStringReference(factoryClassName).Get(),
        IID_PPV_ARGS(&factoryInstance)));
    return factoryInstance;
}

template <typename TInterface>
Microsoft::WRL::ComPtr<TInterface> ActivateInstanceFailFast(_In_z_ PCWSTR className)
{
    ComPtr<TInterface> classInstanceAsInspectable;
    ComPtr<TInterface> classInstance;
    CheckFailure(RoActivateInstance(
        HStringReference(className).Get(),
        &classInstanceAsInspectable));
    CheckFailure(classInstanceAsInspectable.As(&classInstance));
    return classInstance;
}

ComPtr<IUriRuntimeClass> CreateWinRtUri(_In_z_ PCWSTR uri, _In_ bool allowInvalidUri = false)
{
    auto uriRuntimeClassFactory = GetActivationFactoryFailFast<IUriRuntimeClassFactory>(RuntimeClass_Windows_Foundation_Uri);
    ComPtr<IUriRuntimeClass> uriRuntimeClass;
    if (!allowInvalidUri)
    {
        CheckFailure(uriRuntimeClassFactory->CreateUri(HStringReference(uri).Get(), &uriRuntimeClass));
    }
    else
    {
        uriRuntimeClassFactory->CreateUri(HStringReference(uri).Get(), &uriRuntimeClass);
    }
    return uriRuntimeClass;
}

m_processOptions = ActivateInstanceFailFast<IWebViewControlProcessOptions>(RuntimeClass_Windows_Web_UI_Interop_WebViewControlProcessOptions);
ComPtr<IWebViewControlProcessFactory> webViewControlProcessFactory = GetActivationFactoryFailFast<IWebViewControlProcessFactory>(RuntimeClass_Windows_Web_UI_Interop_WebViewControlProcess);
CheckFailure(webViewControlProcessFactory->CreateWithOptions(m_processOptions.Get(), &m_process));
ComPtr<IAsyncOperation<WebViewControl*>> createWebViewAsyncOperation;
CheckFailure(m_process->CreateWebViewControlAsync(reinterpret_cast<INT64>(m_hostWindow), HwndWindowRectToBoundsRect(m_hostWindow), &createWebViewAsyncOperation));
HRESULT hr = createWebViewAsyncOperation->put_Completed(Callback<IAsyncOperationCompletedHandler<WebViewControl*>>([this, createWebViewAsyncOperation](IAsyncOperation<WebViewControl*>*, AsyncStatus status) -> HRESULT
{
    CheckFailure(createWebViewAsyncOperation->GetResults(&m_webViewControl));
    NavigateToUri(L"https://www.bing.com/");
    return S_OK;
}).Get());
CheckFailure(hr);