如何在加载内容之前更改 WebBrowser 控件的背景颜色?
我有一个页面,其中包含经常更新内容的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无能为力,只能在浏览器初始化之前阻止窗口可见。 winforms解决方案:
Not much you can do but prevent the window from getting visible until the browser has initialized itself. A winforms solution:
我也有同样的问题。带有 splitcontainer 和 web 浏览器的表单。而且是非白色背景:-)闪烁令人难以忍受。不管怎样,我通过采用游戏编程方法取得了一些成功;在表单上放置一个计时器,让它每秒调用更新方法 X 次。 update 方法评估几个条件,确定网络浏览器是否可见。当然,更新方法必须简洁明了:
当您检测到调整大小或自己强制调整大小时,您只需隐藏 Web 浏览器以进行 X 更新:
并且当您执行可能导致重新加载的操作时:
并且:
这会将闪烁减少到大约 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:
when you detect a resize or force a resize yourself, you simply hide the webbrowser for X updates:
And when you do something that might cause a reload:
and:
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.