WebBrowser 获取页面大小 [英] WebBrowser get page size
问题描述
在 IE 9 及以上属性上使用 WebBrowser 时
When working with WebBrowser on IE 9 and above the property
webBrowser.Document.Body.ScrollRectangle.Size
始终返回 250 x 250 作为主体尺寸.因此,我无法找到检查当前页面大小的方法.
returns always 250 x 250 as body dimensions. Therefore I can't come across a way to check the current page size.
如何使用 IE 9 及以上版本查看 html 页面的实际大小?
How can I check a html page actual size using IE 9 and above?
推荐答案
我找到了一个似乎一直有效的解决方案.
I've found a solution that seemed to work all the time until now.
我希望它能帮助和我有同样问题的人,正如您在 IE 9 或更高版本中可能已经发现的那样,Body 元素包含 ScrollRectangle 属性的默认大小,但是,正如我发现的那样,一些其他元素确实包含不同大小的 ScrollRectangle 属性.
I hope it'll help people with the same problem as I do, as you might have figured out in IE 9 or above, the Body element contains the default size for the ScrollRectangle property, however, as I found out, some other elements do contain the ScrollRectangle property with different sizes.
可以肯定的是,HTML 元素包含正确的 ScrollRectangle 属性,但其他一些元素可能包含大小更大或更小的 ScrollRectangle,有时它更适合.
Most certainly that the HTML element contains a correct ScrollRectangle property, but some other elements might contain a ScrollRectangle greater or smaller in size, and sometimes it fits better.
所以我得出的结论是,检查 ScrollRectangle 属性的所有元素是最明智的做法,脚本如下:
So I've come to a conclusion that checking all the elements for the ScrollRectangle property is the smartest thing to do, here's the script :
int
CurrentWidth
, CurrentHeight
, CurrentMinWidth = WebBrowserOuterPanel.Width
, CurrentMaxWidth = 0
, CurrentMinHeight = WebBrowserOuterPanel.Height
, CurrentMaxHeight = 0;
foreach(HtmlElement webBrowserElement in webBrowser.Document.All)
{
if ((CurrentWidth = Math.Max(webBrowserElement.ClientRectangle.Width, webBrowserElement.ScrollRectangle.Width)) > CurrentMaxWidth)
CurrentMaxWidth = CurrentWidth;
if ((CurrentHeight = Math.Max(webBrowserElement.ClientRectangle.Height, webBrowserElement.ScrollRectangle.Height)) > CurrentMaxHeight)
CurrentMaxHeight = CurrentHeight;
}
webBrowser.Size = new Size (CurrentMaxWidth > CurrentMinWidth ? CurrentMaxWidth : CurrentMinWidth, CurrentMaxHeight > CurrentMinHeight ? CurrentMaxHeight : CurrentMinHeight);
另一种方式,但可能不正确,实现的想法
Another way but might be not correct, in idea for implementing
HtmlElement webBrowserElement = webBrowser.Document.Body.FirstChild;
CurrentMaxWidth = Math.Max(webBrowserElement.ClientRectangle.Width, webBrowserElement.ScrollRectangle.Width);
CurrentMaxHeight = Math.Max(webBrowserElement.ClientRectangle.Height, webBrowserElement.ScrollRectangle.Height);
这篇关于WebBrowser 获取页面大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!