分享

delphi WebBrowser的使用方法详解(六)

 梦游四海之图书 2018-04-23

通篇引用mshtml;

一、webbrowser获取滚动条的位置

function GetScrollPositionX(FWB:TEmbeddedWB):Integer;     //水平滚动条位置
begin
  result := FWB.OleObject.document.getElementsByTagName('HTML').item(0).ScrollLeft;
end;


function GetScrollPositionY(FWB:TEmbeddedWB):Integer;  //垂直滚动条位置
begin
  result := FWB.OleObject.document.getElementsByTagName('HTML').item(0).ScrollTop;
end;

二、webbrowser获取滚动条的最大位置

function GetScrollPositionXMax(FWB:TEmbeddedWB):Integer;     //水平滚动条最大位置
var
  doc2 : IHTMLDocument2;
  i,i2 : Integer;
begin
  i := FWB.OleObject.document.getElementsByTagName('HTML').item(0).ScrollWidth;
  doc2 := FWB.document as Ihtmldocument2;
  i2 := doc2.Body.getAttribute('offsetWidth', 0);
  result := i-i2;
end;


function GetScrollPositionYMax(FWB:TEmbeddedWB):Integer;  //垂直滚动条最大位置     
var
  doc2 : IHTMLDocument2;
  i,i2 : Integer;
begin
  i := FWB.OleObject.document.getElementsByTagName('HTML').item(0).scrollHeight;
  doc2 := FWB.document as Ihtmldocument2;
  i2 := doc2.Body.getAttribute('offsetHeight', 0);
  if (i-i2=0) and (i+21>FWB.Height) then
    result := i
  else
  result := i-i2;
end;


三、Webbrowser获取元素的位置(矩形)

function GetElementRect(ele : IHTMLElement):IHtmlRect;//
begin
  Result := (ele as ihtmlElement2).getBoundingClientRect ;
end;


四、Webbrowser滚动条滚动到指定位置

    wb1.OleObject.document.parentwindow.scrollby(x,y);  //滚动多少

    wb1.OleObject.document.parentwindow.scrollTo(x,y);//滚动到什么位置


五、Webbrowser判断元素是否存在某一属性
var
  ele: IHTMLElement;
  str_property:string;
begin
      if ele.getAttribute(str_property,0)<>null then
      begin
        showmessage('存在该属性');
     end;  

end;


六、Webbrowser关闭安全警告弹出框

function GetHandleByClassTitle(str_class,str_title: string;bl_extract:Boolean): Thandle;
var
  h: HWnd;
  WndText:String;
  WndClass:string;
  p: array[0..254] of char;
begin
  Result := 0;
  h := GetWindow(Application.Handle, GW_HWNDFIRST);
  while h <> 0 do
  begin
    GetWindowText(h,p,Length(p));
    WndText := p;
    GetClassName(h, p, Length(p));
    WndClass := p;
    if WndClass=str_class then
    begin
      if  bl_extract then
      begin
        if WndText=str_title then
        begin
          Result := h;
        end;
      end
      else
      begin   
        if pos(str_title,WndText)>0  then
        begin   
          Result := h;
        end;
      end;
    end;
    h:=GetWindow(h, GW_HWNDNEXT);
  end;
end;



var
  h: HWnd;
  p: array[0..254] of char;
begin
  h := GetHandleByClassTitle('#32770','安全警告',False);
  if h>0 then
  begin
    SendMessage(h,WM_CLOSE,0,0);
  end;

end;



HTML元素相关属性:

nextSibling 返回当前节点的下一个兄弟节点(只读)
previousSibling 返回此节点的前一个兄弟节点(只读)
parentNode 返回父节点(只读)
childNodes 存储节点的子节点列表(只读)
nodeValue 返回节点的文本(可读写)
Text 返回此节点及其后代的文本内容(可读写)
nodeName 返回节点的名字(只读)

Attributes 存储节点的属性列表(只读)
dataType 返回此节点的数据类型
Definition 以DTD或XML模式给出的节点的定义(只读)
Doctype 指定文档类型节点(只读)
documentElement 返回文档的根元素(可读写)
firstChild 返回当前节点的第一个子节点(只读)
Implementation 返回XMLDOMImplementation对象
lastChild 返回当前节点最后一个子节点(只读)
nodeType 返回节点的类型(只读)
nodeTypedValue 存储节点值(可读写)
ownerDocument 返回包含此节点的根文档(只读)
Parsed 返回此节点及其子节点是否已经被解析(只读)
Prefix 返回名称空间前缀(只读)
preserveWhiteSpace 指定是否保留空白(可读写)
url 返回最近载入的XML文档的URL(只读)
Xml 返回节点及其后代的XML表示(只读)

方法:

getElementsByTagName 返回指定名字的元素集合
appendChild 为当前节点添加一个新的子节点,放在最后的子节点后
createElement 创建一个元素节点
createTextNode 创建包括给定数据的文本节点
removeChild 从子结点列表中删除指定的子节点
hasChildNodes 返回当前节点是否有子节点

cloneNode 返回当前节点的拷贝
createAttribute 创建新的属性
createCDATASection 创建包括给定数据的CDATA段
createComment 创建一个注释节点
createDocumentFragment 创建DocumentFragment对象
createEntityReference 创建EntityReference对象
createNode 创建给定类型,名字和命名空间的节点
createPorcessingInstruction 创建操作指令节点
insertBefore 在指定节点前插入子节点
Load 导入指定位置的XML文档
loadXML 导入指定字符串的XML文档
replaceChild 从子节点列表中替换指定的子节点
Save 把XML文件存到指定节点
selectNodes 对节点进行指定的匹配,并返回匹配节点列表
selectSingleNode 对节点进行指定的匹配,并返回第一个匹配节点
transformNode 使用指定的样式表对节点及其后代进行转换
transformNodeToObject 使用指定的样式表将节点及其后代转换为对象


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多