@@ -72,8 +72,26 @@ class CWebWindow
7272 return result;
7373 }
7474
75- m_webviewController->put_ZoomFactor (zoom);
75+ auto webView2_13 = m_webview.try_query <ICoreWebView2_13>();
76+ if (webView2_13)
77+ {
78+ wil::com_ptr<ICoreWebView2Profile> webView2Profile;
79+ webView2_13->get_Profile (&webView2Profile);
80+ if (webView2Profile)
81+ {
82+ auto webView2Profile2 = webView2Profile.try_query <ICoreWebView2Profile2>();
83+ webView2Profile2->put_PreferredColorScheme (
84+ m_parent->m_bDarkBackgroundEnabled ? COREWEBVIEW2_PREFERRED_COLOR_SCHEME_DARK : COREWEBVIEW2_PREFERRED_COLOR_SCHEME_LIGHT);
85+ }
86+ }
7687
88+ wil::com_ptr<ICoreWebView2Controller2> controller2 = m_webviewController.query <ICoreWebView2Controller2>();
89+ if (controller2)
90+ {
91+ const COREWEBVIEW2_COLOR bg = { 255 , 255 , 255 , 255 };
92+ controller2->put_DefaultBackgroundColor (bg);
93+ }
94+ m_webviewController->put_ZoomFactor (zoom);
7795 m_webviewController->add_AcceleratorKeyPressed (
7896 Callback<ICoreWebView2AcceleratorKeyPressedEventHandler>(
7997 [this ](ICoreWebView2Controller* sender, ICoreWebView2AcceleratorKeyPressedEventArgs* args) {
@@ -268,12 +286,13 @@ class CWebWindow
268286 }
269287
270288 HRESULT Create (IWebDiffWindow* pDiffWindow, HINSTANCE hInstance, HWND hWndParent, const wchar_t * url, const wchar_t * userDataFolder,
271- const SIZE& size, bool fitToWindow, double zoom, std::wstring& userAgent,
289+ const SIZE& size, bool fitToWindow, double zoom, bool darkBackgroundEnabled, std::wstring& userAgent,
272290 IWebDiffCallback* callback, std::function<void (WebDiffEvent::EVENT_TYPE, IUnknown*, IUnknown*)> eventHandler)
273291 {
274292 m_pDiffWindow = pDiffWindow;
275293 m_fitToWindow = fitToWindow;
276294 m_size = size;
295+ m_bDarkBackgroundEnabled = darkBackgroundEnabled;
277296 m_eventHandler = eventHandler;
278297 MyRegisterClass (hInstance);
279298 m_hWnd = CreateWindowExW (0 , L" WinWebWindowClass" , nullptr ,
@@ -1357,6 +1376,20 @@ class CWebWindow
13571376 return m_webmessage;
13581377 }
13591378
1379+ bool IsDarkBackgroundEnabled () const
1380+ {
1381+ return m_bDarkBackgroundEnabled;
1382+ }
1383+
1384+ void SetDarkBackgroundEnabled (bool enabled)
1385+ {
1386+ m_bDarkBackgroundEnabled = enabled;
1387+ DeleteObject (s_hbrBackground);
1388+ s_hbrBackground = CreateSolidBrush (m_bDarkBackgroundEnabled ? RGB (40 , 40 , 60 ) : RGB (206 , 215 , 230 ));
1389+ SetClassLongPtr (m_hWebViewParent, GCLP_HBRBACKGROUND, (LONG_PTR)s_hbrBackground);
1390+ InvalidateRect (m_hWebViewParent, NULL , TRUE );
1391+ }
1392+
13601393private:
13611394
13621395 HRESULT InitializeWebView (const wchar_t * url, double zoom, const std::wstring& userAgent, const wchar_t * userDataFolder, IWebDiffCallback* callback)
@@ -1661,7 +1694,7 @@ class CWebWindow
16611694 wcex.cbWndExtra = 0 ;
16621695 wcex.hInstance = hInstance;
16631696 wcex.hCursor = LoadCursor (nullptr , IDC_ARROW);
1664- wcex.hbrBackground = CreateSolidBrush ( RGB ( 206 , 215 , 230 )) ;
1697+ wcex.hbrBackground = s_hbrBackground ;
16651698 wcex.lpszClassName = L" WebViewParentClass" ;
16661699 }
16671700 return RegisterClassExW (&wcex) != 0 ;
@@ -2083,6 +2116,7 @@ class CWebWindow
20832116 HWND m_hWebViewParent = nullptr ;
20842117 HFONT m_hToolbarFont = nullptr ;
20852118 HFONT m_hEditFont = nullptr ;
2119+ inline static HBRUSH s_hbrBackground = CreateSolidBrush(RGB(206 , 215 , 230 ));
20862120 WNDPROC m_oldTabCtrlWndProc = nullptr ;
20872121 WNDPROC m_oldEditWndProc = nullptr ;
20882122 TOOLINFO m_toolItem{};
@@ -2097,6 +2131,7 @@ class CWebWindow
20972131 std::wstring m_currentUrl;
20982132 std::wstring m_toolTipText = L" test" ;
20992133 bool m_showToolTip = false ;
2134+ bool m_bDarkBackgroundEnabled = false ;
21002135 std::wstring m_webmessage;
21012136 inline static const auto GetDpiForWindowFunc = []() {
21022137 HMODULE hUser32 = GetModuleHandle (L" user32.dll" );
0 commit comments