getting the scroll value from a WebBrowser Control

2020-07-14 05:27发布

问题:

I am trying to get the Y scroll index for a web page in the WebBrowser control but I can't access the values for the built in Scroll bar.

Any Ideas?

回答1:

For IE in standards mode (with a doctype, as you say) scrollTop is a property of the <html> element, not the <body>:

HtmlDocument htmlDoc = this.webBrowser1.Document;
int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;

(A nicer way to get to the <html> element would be good, if anyone knows of one.)



回答2:

are you trying to target an HTML element to bring it into view? If that is what you are after you can do this...

htmlDoc.GetElementById("tag_id_string_goes_here").ScrollIntoView(true);

true aligns it with the top and false with the bottom of the element. You can also use ScrollRectangle to get the dimensions of the scrollable region.



回答3:

WebBrowser1.Document.Body.ScrollTop;
WebBrowser1.Document.Body.ScrollRectangle.Height;