procedure TForm1.Button3Click(Sender: TObject);
  var
      hr:   HRESULT;
      pDisp:   IDispatch;
      pDocument:   IHTMLDocument2;
      pBody:   IHTMLElement;
      pElement:   IHTMLElement2;
  BEGIN
          pDisp   :=   WebBrowser1.Document;
          hr   :=   pDisp.QueryInterface(IID_IHTMLDocument2,   pDocument);
          pBody   :=   pDocument.body;
          hr   :=   pBody.QueryInterface(IID_IHTMLElement2,   pElement);
          edit2.Text :=inttostr(pelement.Get_scrollleft );
  END;
//////////根据论坛搜索到的资料我写了以上的代码,但是 不管怎么移动滚动条,edit2.text中得到的结果始终是‘0’。
我错在哪里?请高手指点迷津!
谢谢!

解决方案 »

  1.   

    关键的地方:ScrollLeft是表示横向滚动条的位置,ScrollTop才是纵向滚动条的位置。
    var
      vElement: IHTMLElement2;
    begin
      if not Assigned(WebBrowser1.Document) then Exit;
      vElement := (WebBrowser1.Document as IHTMLDocument2).Body as IHTMLElement2;
      Edit2.Text := IntToStr(vElement.scrollTop);
    end;代码上就做了点简化而已。
      

  2.   

    某些网站不能成功,比如163.com
      

  3.   

    这几个门户网站 我只成功了 www.sohu.com
      

  4.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>ABC您的英语培训专家</title>
    <style type="text/css">
    <!--
    a:link {
    text-decoration: none;
    }
    a:visited {
    text-decoration: none;
    }
    a:hover {
    text-decoration: none;
    }
    a:active {
    text-decoration: none;
    }
    body {
    margin-top: 0px;
    }
    -->
    </style>
    <link href="css/text.css" rel="stylesheet" type="text/css">
    <link href="css/Onlinestudy.css" rel="stylesheet" type="text/css">
    </head><body>
    <div align="center">
      <br>
      <br>
      <br>
      <table width="100%"  border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td bgcolor="#E6E6E6"><div align="right"><span class="font12_1">  ABC中国首页  </span></div></td>
        </tr>
      </table>
      <table width="98%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td><div align="center">
            <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="750" height="410" align="middle">
                <param name="movie" value="abc-flash2.swf">
                <param name="quality" value="high">
                <embed src="abc-flash2.swf" width="750" height="410" align="middle" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>
            </object>
          </div></td>
        </tr>
      </table>
      <table width="100%"  border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td bgcolor="#E6E6E6"><div align="left"><a href="aboutus/default.asp" target="_blank" class="font12_1">  联系我们</a><span class="font12_1"> | <a href="zhaopinweb/zhaopinzhiwei.html" target="_blank" class="font12_1">ABC招聘</a></span></div></td>
        </tr>
      </table>
      <table width="476" height="44" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td width="227" rowspan="2" bgcolor="#FFFFFF">
            <div align="center" class="font12_1">
              <div align="right"><a href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202003042800007" target="_blank"><img src="oak/new-index/images/biaoshi.gif" width="40" height="48" border="0"></a><a href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202003042800007"></a><br>
              </div>
            </div>
            <div align="center"></div></td>
          <td width="439" bgcolor="#FFFFFF"><div align="left"><span class="font12_1">版权所有:北京径成英教育文化发展有限公司<br>
          办学许可证号:(京)教社证字G9212号 <a href="http://www.miibeian.gov.cn" class="font12_1">京ICP证030067号</a></span></div></td>
        </tr>
        <tr>
          <td height="22" bgcolor="#FFFFFF"><div align="left"><span class="font12_1">Email:<a href="mailto:[email protected]" target="_blank" class="font12_1">[email protected]</a></span></div></td>
        </tr>
      </table>
    </div>
    </body>
    </html>
    网址:http://www.abc.com.cn/
      

  5.   

    不好意思啊,我一直没有刷新网页,所以没有看到您的回复,实在不好意思~
    我的测试网页:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <a href="file:///C|/Documents and Settings/Administrator/My Documents/a.html" target="_blank"><img src="file:///E|/图片/美女/2.jpg" width="1024" height="768" border="0" /></a>
    </body>
    </html>
      

  6.   

    找到一个代码是这样的
    <script type="text/javascript"> 
    // 说明:用 Javascript 获取滚动条位置等信息 
    // 来源 :ThickBox 2.1  
    // 整理 :CodeBit.cn ( http://www.CodeBit.cn ) 
    function getScroll()  

        var t, l, w, h; 
         
        if (document.documentElement && document.documentElement.scrollTop) { 
            t = document.documentElement.scrollTop; 
            l = document.documentElement.scrollLeft; 
            w = document.documentElement.scrollWidth; 
            h = document.documentElement.scrollHeight; 
        } else if (document.body) { 
            t = document.body.scrollTop; 
            l = document.body.scrollLeft; 
            w = document.body.scrollWidth; 
            h = document.body.scrollHeight; 
        } 
        return { t: t, l: l, w: w, h: h }; 

    </script>翻译一下:
    var
      vElement: IHTMLElement2;
    begin
      if not Assigned(WebBrowser1.Document) then Exit;
      vElement :=
        (WebBrowser1.Document as IHTMLDocument3).documentElement as IHTMLElement2;
      if not Assigned(vElement) then
        vElement := (WebBrowser1.Document as IHTMLDocument2).Body as IHTMLElement2;
      Edit2.Text := IntToStr(vElement.scrollTop);
    end;163和楼主给的html测试均可获取。