@@ -27,23 +27,31 @@ applies the color too late.
27
27
``` cpp
28
28
HRESULT AppWindow::CreateControllerWithOptions ()
29
29
{
30
- wil::com_ptr<ICoreWebView2ControllerOptions> options ;
30
+ wil::com_ptr<ICoreWebView2ControllerOptions4> options4 ;
31
31
HRESULT hr = m_environment->CreateCoreWebView2ControllerOptions(&options);
32
32
33
- wil::com_ptr<ICoreWebView2ControllerOptions> stagingOptions;
33
+ if (hr == E_INVALIDARG)
34
+ {
35
+ return S_OK;
36
+ }
37
+
38
+ wil::com_ptr<ICoreWebView2ControllerOptions4> options4;
34
39
auto result = options->QueryInterface(IID_PPV_ARGS(&stagingOptions));
35
40
36
41
if (SUCCEEDED(result))
37
42
{
38
43
COREWEBVIEW2_COLOR wvColor{255, 223, 225, 225};
39
44
stagingOptions->put_DefaultBackgroundColor(wvColor);
40
-
41
- m_environment->CreateCoreWebView2Controller(
42
- m_mainWindow, options.Get(),
43
- Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
44
- this, &AppWindow::OnCreateCoreWebView2ControllerCompleted).Get());
45
45
}
46
46
47
+ m_environment->CreateCoreWebView2Controller(
48
+ m_mainWindow,
49
+ SUCCEEDED (result) ? stagingOptions.Get() : options.Get(),
50
+ Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler >(
51
+ this, &AppWindow::OnCreateCoreWebView2ControllerCompleted).Get());
52
+
53
+ return S_OK;
54
+
47
55
}
48
56
```
49
57
@@ -53,15 +61,16 @@ applies the color too late.
53
61
``` c#
54
62
public MainWindow ()
55
63
{
56
- InitializeComponent ();
57
- SetDefaultBackgroundColor ();
64
+ InitializeComponent ();
65
+ SetDefaultBackgroundColor ();
58
66
}
59
67
60
68
private void SetDefaultBackgroundColor ()
61
69
{
62
- CoreWebView2ControllerOptions options = WebView2 .CoreWebView2 .Environment .CreateCoreWebView2ControllerOptions ();
70
+ CoreWebView2Environment environment = CoreWebView2Environment .CreateAsync ();
71
+ CoreWebView2ControllerOptions options = environment .CreateCoreWebView2ControllerOptions ();
63
72
options .DefaultBackgroundColor = Color .FromArgb (0 , 0 , 255 );
64
- WebView2 .CoreWebView2 . Environment . CreateCoreWebView2ControllerAsync ( parentHwnd , options );
73
+ WebView2 .EnsureCoreWebView2Async ( environment , options );
65
74
}
66
75
67
76
```
@@ -87,15 +96,14 @@ interface ICoreWebView2ControllerOptions4 : IUnknown {
87
96
///
88
97
/// The ` DefaultBackgroundColor ` is the color that renders underneath all web
89
98
/// content. This means WebView renders this color when there is no web
90
- /// content loaded. It is important to note that the default color is white.
99
+ /// content loaded. When no background color is defined in WebView2, it uses
100
+ /// the ` DefaultBackgroundColor ` property to render the background.
101
+ /// By default, this color is set to white.
102
+ ///
91
103
/// Currently this API only supports opaque colors and transparency. It will
92
104
/// fail for colors with alpha values that don't equal 0 or 255 ie. translucent
93
- /// colors are not supported. It also does not support transparency on Windows 7.
94
- /// On Windows 7, setting DefaultBackgroundColor to a Color with an Alpha value
95
- /// other than 255 will result in failure. On any OS above Win7, choosing a
96
- /// transparent color will result in showing hosting app content. This means
97
- /// webpages without explicit background properties defined will render web
98
- /// content over hosting app content.
105
+ /// colors are not supported. When WebView2 is set to have a transparent background,
106
+ /// it renders the content of the parent window behind it.
99
107
100
108
[ propget] HRESULT DefaultBackgroundColor([ out, retval] COREWEBVIEW2_COLOR* value);
101
109
[ propput] HRESULT DefaultBackgroundColor([ in] COREWEBVIEW2_COLOR value);
0 commit comments