Open
Description
I know that we can set the background colour on demand like this:
void CWebBrowser::SetBackgroundColor(COLORREF colour, bool transparent)
{
COREWEBVIEW2_COLOR webViewColour{};
webViewColour.R = GetRValue(colour);
webViewColour.G = GetGValue(colour);
webViewColour.B = GetBValue(colour);
webViewColour.A = transparent ? 0 : 255;
wil::com_ptr<ICoreWebView2Controller2> controller2 =
m_pImpl->m_webController.query<ICoreWebView2Controller2>();
controller2->put_DefaultBackgroundColor(webViewColour);
}
And, I know that a recent update to WebView2 allows us to set the default background colour. ChatGPT suggests the following approach to using this new API:
#include <wrl.h>
#include <WebView2.h>
using namespace Microsoft::WRL;
void CreateWebView2WithCustomBackgroundColor(HWND hWnd)
{
ComPtr<ICoreWebView2Environment> webViewEnvironment;
// First, create the WebView2 environment
HRESULT hr = CreateCoreWebView2EnvironmentWithOptions(
nullptr, nullptr, nullptr,
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
[hWnd](HRESULT result, ICoreWebView2Environment* env) -> HRESULT
{
if (FAILED(result)) return result;
ComPtr<ICoreWebView2Environment3> env3;
if (SUCCEEDED(env->QueryInterface(IID_PPV_ARGS(&env3))))
{
// Create controller options
ComPtr<ICoreWebView2ControllerOptions> options;
HRESULT hr = env3->CreateCoreWebView2ControllerOptions(&options);
if (FAILED(hr)) return hr;
// Set default background color via ICoreWebView2ControllerOptions3
ComPtr<ICoreWebView2ControllerOptions3> options3;
if (SUCCEEDED(options.As(&options3)))
{
COREWEBVIEW2_COLOR bgColor = { 255, 30, 30, 30 }; // A, R, G, B
options3->put_DefaultBackgroundColor(bgColor);
}
// Create controller with options
return env->CreateCoreWebView2ControllerWithOptions(
hWnd, options.Get(),
Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
[](HRESULT result, ICoreWebView2Controller* controller) -> HRESULT
{
if (!controller) return result;
ComPtr<ICoreWebView2> webview;
controller->get_CoreWebView2(&webview);
// Your logic with webview here
return S_OK;
}).Get());
}
return E_NOINTERFACE;
}).Get());
}
But I am not 100% sure that this is the right approach. So far, I have created the required environment in my EvenmentCompleted handler:
HRESULT CWebBrowser::OnCreateEnvironmentCompleted(
HRESULT result,
ICoreWebView2Environment* environment)
{
CHECK_FAILURE(result);
if (!environment)
return E_FAIL;
CHECK_FAILURE(environment->QueryInterface(IID_PPV_ARGS(&m_pImpl->m_webViewEnvironment)));
CHECK_FAILURE(environment->QueryInterface(IID_PPV_ARGS(&m_pImpl->m_webViewEnvironment2)));
CHECK_FAILURE(environment->QueryInterface(IID_PPV_ARGS(&m_pImpl->m_webViewEnvironment3)));
// I need to add the correct code here to set the background.
CHECK_FAILURE(m_pImpl->m_webViewEnvironment->CreateCoreWebView2Controller(
m_hWnd,
Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
this,
&CWebBrowser::OnCreateWebViewControllerCompleted).Get()));
return S_OK;
}
Where m_webViewEnvironment3
is defined as wil::com_ptr<ICoreWebView2Environment3> m_webViewEnvironment3;
. But I can't see in the official sample an example of using the new API. Nor here . Sorry if I have overlooked a document explain this.
Metadata
Metadata
Assignees
Labels
No labels