How to locate and highlight a string in a TWebBrowser

类别:Delphi 点击:0 评论:0 推荐:

uses mshtml;

procedure WBLocateHighlight(WB: TWebBrowser; Text: string) ;
const
   prefix = '<span style="color:white; background-color: red;">';
   suffix = '</span>';
var
   tr: IHTMLTxtRange;
begin
   if Assigned(WB.Document) then
   begin
     tr := ((wb.Document AS IHTMLDocument2).body AS IHTMLBodyElement).createTextRange;
     while tr.findText(Text, 1, 0) do
     begin
       tr.pasteHTML(prefix + tr.htmlText + suffix) ;
       tr.scrollIntoView(True) ;
     end;
   end;
end;

Usage:
WBLocateHighlight(WebBrowser1,'delphi') ;
~~~~~~~~~~~~~~~~~~~~~~~~~

本文地址:http://com.8s8s.com/it/it4788.htm