Skip to content

Can I remove the white rectangle when initializing the WebView2? #5123

Open
@ajtruckle

Description

On my second tab page I have a browser control, and I initialize the tab control when the tab is selected:

Recording.2025-02-23.114147.mp4

Code:

void CAboutDlg::OnTabSelected(TabCtrl* ctrl, TabCtrl::HTAB tab)
{
	const int sel = ctrl->GetTabIndexByHandle(tab);

	if (sel == 1)
	{
		m_pageCredits.InitialiseWebPreview2();

		return;
	}
}

And in the tab class:

void CCreditsPage::InitialiseWebPreview2()
{
	if (m_pWebBrowser) return;

	m_lblHtmlPreview.GetWindowRect(m_rctHtmlPreview);
	ScreenToClient(m_rctHtmlPreview);

	m_pWebBrowser = std::make_unique<CWebBrowser>();
	if (m_pWebBrowser != nullptr)
	{
		m_pWebBrowser->CreateAsync(
			WS_VISIBLE | WS_CHILD,
			m_rctHtmlPreview,
			this,
			1,
			[&]() {
				m_pWebBrowser->SetParent(this);
				m_pWebBrowser->EnableContextMenus(false);
				m_pWebBrowser->SetPreferredColorScheme(IsUsingDarkMode()
					? COREWEBVIEW2_PREFERRED_COLOR_SCHEME_DARK
					: COREWEBVIEW2_PREFERRED_COLOR_SCHEME_LIGHT
				);
				m_pWebBrowser->Navigate(theApp.GetCommonAppDataFolder() + L"credits.html", nullptr);
			});
	}
}

If you watch the video you will see the momentary white rectangle. How can I overcome this?

Note that I am using a media query to render the HTML in dark mode:

        /* Dark mode for screens only */
        @media screen and (prefers-color-scheme: dark) {
            body {
                background-color: #121212;
                color: #e0e0e0;
            }

            h1 {
                color: #ffffff;
                border-color: #ffffff;
            }

            h2 {
                background-color: #1e1e1e;
                color: #ffffff;
            }

            table {
                border-color: #ffffff;
            }

            td, th {
                border-color: #ffffff;
                background-color: #1e1e1e;
                color: #e0e0e0;
            }

            table tbody tr:hover td {
                background: #333333;
                color: #ffffff;
            }

            .container {
                background-color: #1e1e1e;
                color: #e0e0e0;
            }

            a {
                color: #8ab4f8;
            }

                a:hover {
                    color: #ffab40;
                }

            li {
                color: #e0e0e0;
            }

            hr {
                border-color: #ffffff;
            }
        }

Thank you for your help.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions