如何读取WebBrowser控件的当前和最大滚动位置?

时间:2011-01-28 18:07:26

标签: .net winforms scroll webbrowser-control

我可以使用WebBroswer.Document.Window.ScrollTo()设置滚动位置,但如何读取当前滚动位置?另外,如何阅读文档的总高度?

背景:我想让WebBrowser显示日志以在新消息进入时自动向下滚动,但前提是它已经一直滚动到底部,以便用户可以向上滚动检查旧消息没有中断。

1 个答案:

答案 0 :(得分:2)

我无法直接向您提供WinForms版本,但我可以为您提供我们在托管代码中使用的版本。

但我真正想说的是你必须小心知道你的文件是否在

  • 标准模式:即Internet Explorer 6或更新版
  • 怪癖模式:即Internet Explorer 5.5兼容模式

您想知道这一点的原因是您要查询的属性取决于浏览器是标准模式还是怪异模式。怪癖模式下的文档不支持documentElement.scrollHight(即它返回零)。

                                       returns the height of...
Expression                             Quirks Mode                 Standards Mode
=====================================  ==========================  ==========================
document.body.scrollHeight             document                    body
document.documentElement.scrollHeight  (not supported)             document

希望 文档高度,因此返回的方式是:

  • 标准模式:document.documentElement.scrollHeight(返回文档高度)
  • Quirks模式:document.body.scrollHeight(实际返回文档的高度,而不是正文)

所以我实际上有两个辅助函数:

int WebBrowserScrollHeightQuirksMode(WebBrowser wb);

int WebBrowserScrollHeightStandardsMode(WebBrowser wb);

这迫使你弄清楚你的html是否处于 Netscape 2.0 兼容模式,并找出你想要调用哪一个:

function WebBrowserScrollHeightQuirksMode(const WebBrowser: TWebBrowser): Integer;
var
    doc: IHTMLDocument2;
begin
   {
       Expression                             Mode       Returns ScrollHeight of
       document.body.scrollHeight             Quirks     document
       document.body.scrollHeight             Standards  body

       document.documentElement.scrollHeight  Standards  document

       We *want* the scroll height of the document, which means we should have been
       using document.documentElement.scrollHeight

       WARNING: A document in quirks mode does not support documentElement[scrollHight]
                i.e. it returns zero
   }

   doc := (WebBrowser.Document as IHTMLDocument2);
   if not Assigned(doc) then
      Result := 0
   else if not Assigned(doc.body) then
      Result := 0
   else
      Result := doc.body.getAttribute('scrollHeight', 0);
end;

HTML处于标准模式时的版本(即Internet Explorer 6或更新版本):

function WebBrowserScrollHeightStandardsmode(const WebBrowser: TWebBrowser): Integer;
var
doc: IHTMLDocument2;    //ie4, has doc.body
doc3: IHTMLDocument3;   //ie5, has doc.documentElement
begin
   //Use this for IE6 or newer, with the browser in standards mode
   Result := 0;
   {
       Expression                             Mode       Returns ScrollHeight of
       =====================================  =========  =======================
       document.body.scrollHeight             Quirks     document
       document.body.scrollHeight             Standards  body
       document.documentElement.scrollHeight  Standards  document

       We *want* the scroll height of the document, which means we should have been
       using document.documentElement.scrollHeight

       WARNING: A document in quirks mode does not support documentElement[scrollHight]
                i.e. it returns zero
   }

   doc := (WebBrowser.Document as IHTMLDocument2);
   if not Assigned(doc) then
      Exit;

   //Should use doc3.documentElement scrollHeight
   if Supports(doc, IHTMLDocument3, doc3) then //Requires Windows 95, IE5
   begin
      if Assigned(doc3.documentElement) then
      begin
         Result := VarAsInteger(doc3.documentElement.getAttribute('scrollHeight', 0));
         Exit;
      end;
   end;
end;