Tuesday, July 3, 2012

LOCATE AND HIGHLIGHT A STRING IN A TWEBBROWSER

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') ;

0 comments:

Post a Comment

 
Related Posts Plugin for WordPress, Blogger...