gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:20:20 26 4
gpt4 key购买 nike

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

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

最佳答案

这是 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);

关于c++ - 如何在MFC或C++中实例化IWebViewControl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52037181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com