如何在.NET中检索webbrowser控件的滚动条位置

Nie*_* H. 6 c#

我尝试使用webBrowser1.Document.Body.ScrollTopwebBrowser1.Document.Body.ScrollLeft,但它们不起作用.他们总是返回0,我无法访问webBrowser1.Document.documentElement.ScrollTop.ScrollLeft.

Mar*_*arc 13

好的,我解决了它:

Dim htmlDoc As HtmlDocument = wb.Document
Dim scrollTop As Integer = htmlDoc.GetElementsByTagName("HTML")(0).ScrollTop
Run Code Online (Sandbox Code Playgroud)


Jef*_*tes 5

为了实际滚动,我们发现该ScrollIntoView方法运行良好.例如,滚动到页面的左上角.

 this.webBrowser.Document.Body.FirstChild.ScrollIntoView(true);
Run Code Online (Sandbox Code Playgroud)

但是,我们没有成功地获得滚动位置(也就是说,我们没有花太多时间尝试).如果您控制HTML内容,可以考虑使用一些javascript将滚动位置复制到隐藏元素中,然后使用DOM读取该值.

ScrollTop并且ScrollLeft仅允许在元素的边界与其内容之间提供偏移.似乎无法通过这些值操纵滚动.相反,你必须使用ScrollIntoView.