TWebBrowser加载的HTML文档总高度

4

我希望知道如何获取加载到TWebBrowser组件(Delphi)中的html文档的总高度?

我已经找到了类似于以下内容,但并不起作用:

webbrowser.oleobject.document.body.scrollheight

我把它放在OnDocumentComplete事件里。

我需要高度,因为我正在计算ScrollBar的PageSize属性(我的自定义ScrollBar - 内置WebBrowser已禁用),该属性取决于网页的高度。

感谢任何反馈,最好的问候


请定义“it is not working”。 - kobik
看一下我的评论:https://dev59.com/tIfca4cB1Zd3GeqPhFCW?noredirect=1#comment44743633_28187583使用我提出的代码版本,我得到了相同的结果。 - kuta_senator
1个回答

2

像这样的代码应该可以正常工作:

uses MSHTML;

var
  HtmlElement: IHTMLElement2;
  PageHeight: Integer;

begin
  with MyWebBrowser.ControlInterface do
  begin
    HtmlElement := (Document as IHTMLDocument3).documentElement as IHTMLElement2;
  end;

  PageHeight := HtmlElement.scrollHeight;
end;

这是完整的高度。由于边距(margins)的原因,body元素似乎给出了略小的值:
var
  BodyElement: IHTMLElement2;
  PageHeight: Integer;

begin
  with MyWebBrowser.ControlInterface do
  begin
    BodyElement := (Document as IHTMLDocument2).body as IHTMLElement2;
  end;

  PageHeight := BodyElement.scrollHeight;
end;

奇怪的是,每次加载新页面时它都会给我一个固定值(int),现在它给我整数1633572(其中浏览器高度大约为600px,文档稍微高一点 - 大约650-700px)。我在面板上放置了Web浏览器,并将此面板放置在第二个更高的panel2上。不知道为什么它不起作用... - kuta_senator
嗯,看起来第二个选项是有效的,但正如你所说,body 的高度比 documentElement 小一点,但 documentElement 给我错误的高度值(每次都是常数)。 - kuta_senator

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接