I'm not sure if I can explain this clearly.
I have some simple HTML code and am loading it to the Twebbrowser. Then, I want to get the Rect of my HTML output so that i could proper re-size the height 开发者_C百科of my Twebbrowser.
It's like auto-size feature, but it's only the height that auto-size. is that possible?
thanx in advance...
This code should get you started:
uses MSHTML, Math;
var
HtmlElement, BodyElement:IHTMLElement2;
PageWidth, PageHeight: Integer;
begin
with WebBrowser1.ControlInterface do
begin
HtmlElement:=(Document as IHTMLDocument3).documentElement as IHTMLElement2;
BodyElement:=(Document as IHTMLDocument2).body as IHTMLElement2;
end;
PageWidth:= Max(BodyElement.scrollWidth, HtmlElement.scrollWidth);
PageHeight:= Max(BodyElement.scrollHeight, HtmlElement.scrollHeight);
end;
I read the dimensions both from the HTML element and the BODY element and take the maximum because often they have different dimensions. And even more often one of them returns size 0x0. This behavior also changes from browser version to browser version. It's a workaround, you have to check if this is the right way to deal with it in your use case.
Note: the image @Darkerstar posted was the old MSDN illustration for getting element dimensions. The updated version (for IE9) seems to be this one: Measuring Element Dimension and Location with CSSOM in Internet Explorer 9.
You can work out the page height inside TWebBrowser's document complete event, using document.body.scrollWidth and scrollHeight
精彩评论