自定义TWebBrowser浏览器控制遇到的一些问题

来源:互联网 发布:黑马程序员论坛 编辑:程序博客网 时间:2024/05/17 03:44

 最近工作需要,要将TWebBrowser样式改头换面,包括菜单,滚动条等,都要换掉。
 由于滚动条已经屏蔽掉,所以关于页面滚动等,全部都需要自己写代码来模拟了。模拟的过程中发现获得页面的客户区大小ClientHeight,ClientWidth以及页面总大小ScrollHeight,ScrollWidth的时候,还有滚动条滑块位置ScrollTop,ScrollLeft的值总是有不对的情况,通过在网上查找才发现,原来是W3C标准在作怪,只要是使用了W3C标准格式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
作为开头的,我使用
((FWebBrowser.Document as IHTMLDocument2).body as IHTMLElement2).scrollTop这些获得的值都有问题,始终为0
而不用该格式的,都不会有问题。
那么使用了该格式的,用什么方法来获得呢!通过资料发现
FWebBrowser.oleobject.document.documentelement.ClientHeight;这样的方式来获得标准格式的各个值。
资料原文如下

  1. JavaScript:
  2. function IeTrueBody(){//判断头部是否有标准声明
  3.     return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
  4. }

通过这里我们可以看到,需要检查是否是标准文档,这个就需要document.compatMode的判断
资料如下:

转载:http://www.cnscn.org(CNS电脑与英语学习网)

声明:我写的博客就是我的学习笔记,让我在忘记的时候可以在任何时候,任何地方,打开连接就可以看到,让我想起忘记的东西.
我的博客通常都有资料来源,只用于学习,没有任何其他目的,如果有版权问题,请跟我联系,如果对资料有什么疑问,可以留言!
我的email:herhun@163.com

对于document.compatMode,很多朋友可能都根我一样很少接触,知道他的存在却不清楚他的用途。今天在ext中看到 document.compatMode的使用,感觉这个对于我们开发兼容性的web页面还是很有帮助,我们都知道,IE对盒模型的渲染在 Standards Mode和QuirksMode是有很大差别的,在Standards Mode下对于盒模型的解释和其他的标准浏览器是一样,但在QuirksMode模式下则有很大差别,而在不声明Doctype的情况下,IE默认又是QuirksMode。所以为兼容性考虑,我们可能需要获取当前的文档渲染方式。
      document.compatMode正好派上用场,它有两种可能的返回值:BackCompat和CSS1Compat,对其解释如下:
BackCompat Standards-compliant mode is not switched on. (Quirks Mode)
CSS1Compat Standards-compliant mode is switched on. (Standards Mode)

     在实际的项目中,我们还需要在获取浏览是否IE,这样就可以得到IE的渲染模式了。在Ext中的代码:isBorderBox=isIE&&!isStrict。



当文档有了标准声明时, document.compatMode 的值就等于 "CSS1compat", 因此, 我们可以根据 document.compatMode 的值来判断文档是否加了标准声明

var height = document.compatMode=="CSS1Compat" ? document.documentElement.clientHeight : document.body.clientHeight;

所以,滚动控制就要加上这样一个判断,如果是标准的就使用
FWebBrowser.oleobject.document.documentelement.ClientHeight
否则就使用另一种方式:

DelphiCode:
 procedure TDxSkinWebBrowser.ReSetWebSize; var   WebClientWidth,WebClientHeight: Integer;   MaxScrollWidth, MaxScrollHeight: Integer;   webPoint: TPoint; begin   if (FWebBrowser.Document <> nil) and ((FWebBrowser.Document  as IHTMLDocument2).body <> nil) then   begin     {先获得页面的最大高度}     if (FScrollBars = ssVertical) or (FScrollBars = ssBoth) then     begin       if IsW3CStd then //如果是W3C标准          MaxScrollHeight := FWebBrowser.oleobject.document.documentelement.ScrollHeight       else          MaxScrollHeight := (FWebBrowser.Document  as IHTMLDocument2).body.getAttribute('ScrollHeight', 0);       if MaxScrollHeight > FWebBrowser.Height  then //总高度大于显示高度度出现滚动条       begin          if IsW3CStd then          begin            WebClientHeight := FWebBrowser.oleobject.document.documentelement.ClientHeight;            webPoint.Y := FWebBrowser.oleobject.document.documentelement.scrollTop;          end          else          begin            WebClientHeight := (FWebBrowser.Document as IHTMLDocument2).body.getAttribute('ClientHeight', 0);            webPoint.Y := ((FWebBrowser.Document as IHTMLDocument2).body as IHTMLElement2).scrollTop;          end;          FVScrollBar.SetRange(0,MaxScrollHeight,webPoint.Y,WebClientHeight);          FVScrollBar.Visible := true;       end else FVScrollBar.Visible := False;     end;     if (FScrollBars = ssHorizontal) or (FScrollBars = ssBoth)  then     begin       if IsW3CStd then //如果是W3C标准         MaxScrollWidth := FWebBrowser.oleobject.document.documentelement.ScrollWidth       else         MaxScrollWidth := (FWebBrowser.Document as IHTMLDocument2).body.getAttribute('ScrollWidth', 0);       //总宽度大于显示宽度,出现滚动条       if MaxScrollWidth > FWebBrowser.Width then       begin         if IsW3CStd then         begin           WebClientWidth := FWebBrowser.oleobject.document.documentelement.ClientWidth;           webPoint.x := FWebBrowser.oleobject.document.documentelement.ScrollLeft;         end         else         begin           WebClientWidth := (FWebBrowser.Document as IHTMLDocument2).body.getAttribute('clientWidth', 0);           webPoint.x := ((FWebBrowser.Document as IHTMLDocument2).body as IHTMLElement2).scrollLeft;         end;         FHScrollBar.SetRange(0,MaxScrollWidth,webPoint.X,WebClientWidth);         FHScrollBar.Visible := True;       end else FHScrollBar.Visible := False;     end;   end;end;