如何在加载内容之前更改 WebBrowser 控件的背景颜色?

发布于 2024-12-12 18:08:21 字数 325 浏览 0 评论 0原文

我有一个页面,其中包含经常更新内容的 WebBrowser 控件。我使用的是黑色背景,但加载 Web 浏览器内容之前的默认颜色是白色。我可以通过加载一个小的 HTML 字符串并将背景设置为黑色来更改它,但仍有一段时间 Web 浏览器显示为白色,因此会发生某种闪烁效果。

我的问题是:有没有办法改变WebBrowser底层控件的颜色?

我尝试了一些解决方案,例如隐藏 WebBrowser 直到内容加载完毕,但这些解决方案都感觉不太优雅,并且效果不佳。

注意:这个确切的问题之前已经针对 WP7/Silverlight 提出并回答过,但我需要一个针对 WPF/Winforms 的解决方案。

I have a page that contains a WebBrowser control that is frequently updating content. I am using a black background, but the default color before loading content of the WebBrowser is white. I can change it by loading a small HTML string with the background set to black but there is still a period of time when the WebBrowser appears as white so there is a sort of a flickering effect happening.

My question is this: is there a way to change the color of the underlying control of the WebBrowser?

I have tried a few solutions such as hiding the WebBrowser until the content has been loaded but none of these feel very elegant and don't work all that well.

Note: This exact question has been asked and answered before for WP7/Silverlight, but I need a solution for WPF/Winforms.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无法言说的痛 2024-12-19 18:08:21

您无能为力,只能在浏览器初始化之前阻止窗口可见。 winforms解决方案:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    protected override void SetVisibleCore(bool value) {
        if (!this.IsHandleCreated) {
            CreateHandle();
            value = false;
            webBrowser1.DocumentCompleted += initializationDone;
            webBrowser1.DocumentText = "<body bgcolor=#000000></body>";
        }
        base.SetVisibleCore(value);
    }
    private void initializationDone(object sender, WebBrowserDocumentCompletedEventArgs e) {
        webBrowser1.DocumentCompleted -= initializationDone;
        this.Visible = true;
    }
}

Not much you can do but prevent the window from getting visible until the browser has initialized itself. A winforms solution:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    protected override void SetVisibleCore(bool value) {
        if (!this.IsHandleCreated) {
            CreateHandle();
            value = false;
            webBrowser1.DocumentCompleted += initializationDone;
            webBrowser1.DocumentText = "<body bgcolor=#000000></body>";
        }
        base.SetVisibleCore(value);
    }
    private void initializationDone(object sender, WebBrowserDocumentCompletedEventArgs e) {
        webBrowser1.DocumentCompleted -= initializationDone;
        this.Visible = true;
    }
}
仙女 2024-12-19 18:08:21

我也有同样的问题。带有 splitcontainer 和 web 浏览器的表单。而且是非白色背景:-)闪烁令人难以忍受。不管怎样,我通过采用游戏编程方法取得了一些成功;在表单上放置一个计时器,让它每秒调用更新方法 X 次。 update 方法评估几个条件,确定网络浏览器是否可见。当然,更新方法必须简洁明了:

    private void OnPollForChanges(FormBrowseAllData inData)
    {

        webBrowser1.Visible = (mWebBrowserResizing == 0) && 
                    (!mWebBrowserLoading) && 
                    webBrowser1.ReadyState >= WebBrowserReadyState.Interactive;

        if (mWebBrowserResizing>0)
        {
            mWebBrowserResizing--;
        }

当您检测到调整大小或自己强制调整大小时,您只需隐藏 Web 浏览器以进行 X 更新:

    private void webBrowser1_Resize(object sender, EventArgs e)
    {
        mWebBrowserResizing = 5;
        webBrowser1.Visible = false;
    }

并且当您执行可能导致重新加载的操作时:

    private void SetHTML(string inHTML)
    {
        webBrowser1.Visible = false;
        mWebBrowserLoading = true;
        webBrowser1.DocumentText = inHTML;
    }

并且:

    void OnWebBrowserDocumentCompleted(object sender, 
            WebBrowserDocumentCompletedEventArgs e)
    {
        mWebBrowserLoading = false;
    }

这会将闪烁减少到大约 5是之前的%。当然,这些只是蹩脚的技巧,而不是正确的解决方案。

PS:mWebBrowserLoading的使用和检查webBrowser1.ReadyState之间有一些重叠。因为理论上 ReadyState 应该与 mWebBrowserLoading 同步变化,但无论如何“安全总比后悔好”是这里的座右铭。

I had the same problem. A form with a splitcontainer and a webbrowser. And a non-white background :-) The flickering was unbearable. Anyway, I had some success by taking the game-programming approach; put a timer on your form, let it call an update method X times per second. The update method evaluates a couple of conditions which determine if the webbrowser should be made visible. Of course the update method must be lean and mean:

    private void OnPollForChanges(FormBrowseAllData inData)
    {

        webBrowser1.Visible = (mWebBrowserResizing == 0) && 
                    (!mWebBrowserLoading) && 
                    webBrowser1.ReadyState >= WebBrowserReadyState.Interactive;

        if (mWebBrowserResizing>0)
        {
            mWebBrowserResizing--;
        }

when you detect a resize or force a resize yourself, you simply hide the webbrowser for X updates:

    private void webBrowser1_Resize(object sender, EventArgs e)
    {
        mWebBrowserResizing = 5;
        webBrowser1.Visible = false;
    }

And when you do something that might cause a reload:

    private void SetHTML(string inHTML)
    {
        webBrowser1.Visible = false;
        mWebBrowserLoading = true;
        webBrowser1.DocumentText = inHTML;
    }

and:

    void OnWebBrowserDocumentCompleted(object sender, 
            WebBrowserDocumentCompletedEventArgs e)
    {
        mWebBrowserLoading = false;
    }

This reduces flicker to about 5% of what it was before. Of course these are just lame hacks and not proper solutions.

PS: there is some overlap between the use of mWebBrowserLoading and checking webBrowser1.ReadyState. Because in theory ReadyState should change in sync with mWebBrowserLoading, but anyway "Better safe than sorry" is the motto here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文