How to retrieve the scrollbar position of the webbrowser control in .NET

前端 未结 7 1813
刺人心
刺人心 2020-12-11 18:44

I tried using webBrowser1.Document.Body.ScrollTop and webBrowser1.Document.Body.ScrollLeft, but they don\'t work. They always return 0 and I can\'t

7条回答
  •  Happy的楠姐
    2020-12-11 19:01

    Accepted answer is VB. For C# WPF WebBrowser, I had to write the following. No idea if I really need all those casts or not. If one can get rid of any of those casts, that would be terrific.

    using mshtml;
    int? GetScrollTop(System.Windows.Controls.WebBrowser browser)
    {
      object doc = browser.Document;
      HTMLDocument castDoc = doc as HTMLDocument;
      IHTMLElementCollection elements = castDoc?.getElementsByTagName("HTML");
      IEnumerator enumerator = elements?.GetEnumerator();
      enumerator?.MoveNext();
      var first = enumerator?.Current;
      IHTMLElement2 castFirst = first as IHTMLElement2;
      int? top = castFirst?.scrollTop;
      return top;
    }
    

提交回复
热议问题