开发者

Is there a function to get the client rect inside the Twebbrowser?

开发者 https://www.devze.com 2023-03-23 05:38 出处:网络
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 heigh

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

Is there a function to get the client rect inside the Twebbrowser?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号