作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
最近 Microsoft 为 WPF 和 winforms 引入了 IWebViewControl
。IWebViewControl
使用 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/
我是一名优秀的程序员,十分优秀!