【问题标题】:WebBrowser get page sizeWebBrowser 获取页面大小
【发布时间】:2018-08-01 17:21:56
【问题描述】:

在 IE 9 及更高版本上使用 WebBrowser 时

webBrowser.Document.Body.ScrollRectangle.Size

始终返回 250 x 250 作为主体尺寸。 因此,我无法找到检查当前页面大小的方法。

如何使用 IE 9 及更高版本查看 html 页面的实际大小?

【问题讨论】:

    标签: c# wpf webbrowser-control


    【解决方案1】:

    我找到了一个似乎一直有效的解决方案。

    我希望它能帮助和我遇到同样问题的人,正如您可能已经在 IE 9 或更高版本中发现的那样,Body 元素包含 ScrollRectangle 属性的默认大小,但是,正如我发现的那样,有些其他元素确实包含不同大小的 ScrollRectangle 属性。

    可以肯定的是,HTML 元素包含正确的 ScrollRectangle 属性,但其他一些元素可能包含大小更大或更小的 ScrollRectangle,有时它更适合。

    所以我得出结论,检查 ScrollRectangle 属性的所有元素是最明智的做法,这是脚本:

    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);
    

    另一种方式,但可能不正确,用于实施的想法

    HtmlElement webBrowserElement = webBrowser.Document.Body.FirstChild;
    
    CurrentMaxWidth = Math.Max(webBrowserElement.ClientRectangle.Width, webBrowserElement.ScrollRectangle.Width);
    
    CurrentMaxHeight = Math.Max(webBrowserElement.ClientRectangle.Height, webBrowserElement.ScrollRectangle.Height);
    

    【讨论】:

      【解决方案2】:

      这应该让你得到窗口大小

      webBrowser.Document.Window.Size

      【讨论】:

      • 所以你可以试试这个:WebBrowser.Document.Body.ScrollRectangle.Size 如果这不起作用,页面可能已经完成渲染,可能需要使用 DocumentComplete
      • 如果可以的话,下次有人问这个问题时,您可以观看我的解决方案。感谢您愿意提供帮助。
      猜你喜欢
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 2011-02-25
      • 2020-03-02
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多