Delphi中编程实现TWebBrowser查找及上一个、下一个功能

  代码比较简单,全部内容如下:
//1:向下搜索;-1:向上搜索
procedure TFrm_SearchEx.doSearch(iDir:integer;keyword:String)   ;
var
  doc:IHtmlDocument2;
  txtRange:IHtmlTxtRange;
begin
  doc := self.WebBrowser.Document as IHtmlDocument2;
  txtRange := doc.selection.createRange as IHtmlTxtRange;

  //获取当前选取区域
  if txtRange.text='' then   begin
   doc.execCommand(   'SelectAll',false,EmptyParam);
   txtRange := doc.selection.createRange as IHtmlTxtRange;
   txtRange.findText(keyword,iDir,0);
  end
  else begin
   if iDir=1 then begin
     txtRange.collapse(false);
     txtRange.moveEnd('textedit',1)   ;//specifies the number of units to move.
     txtRange.findText(keyword,1,0);
   end;
   if iDir=-1 then begin
     txtRange.collapse(true);
     txtRange.moveStart('textedit',-1)   ;//specifies the number of units to move.
     txtRange.findText(keyword,-1,0);
    end;

  end;


  txtRange.select;


  //self.WebBrowser.OleObject.document.getElementById('a').select
end;

你可能感兴趣的:(Delphi,TwebBrowser)