Re: 'Text search' with Delphi TWebBrowser

Posted by webmaster Guido on April 06, 2004

In Reply to: 'Text search' with TWebBrowser posted by Giovanni Caramia p12316 on April 06, 2004

: I showld like implement a 'Search Text' for find words in html page displaied in TWebBrowser (D7).

The easiest is to use Windows' own Find dialog for the browser :)
(works even in older Delphi versions like D4, that don't have a TWebBrowser on the component panel, where it is imported as ActiveX)

Example: the code below shows how clicking Button1 opens the browser Find dialog, and that works exactly like you are used to in your IE browser.

Important: add "ACTIVEX" to the "uses" clause of your unit.

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Replace WebBrowser1 with name of
  // your webbrowser: WB1, MyBrowser or whatever 
  OpenFindDialog(WebBrowser1); 
end;
procedure TForm1.OpenFindDialog(WB: TWebBrowser);
const
  CGID_WebBrowser: TGUID = '{ED016940-BD5B-11cf-BA4E-00C04FD70816}';
  HTMLID_FIND = 1;
var
  CmdTarget : IOleCommandTarget;
  vaIn, vaOut: OleVariant;
  PtrGUID: PGUID;
begin
  New(PtrGUID);
  PtrGUID^ := CGID_WebBrowser;
  if WB.Document <> nil then
    try
      WB.Document.QueryInterface(IOleCommandTarget, CmdTarget);
      if CmdTarget <> nil then
        try
          CmdTarget.Exec(PtrGUID, HTMLID_FIND, 0, vaIn, vaOut);
        finally
          CmdTarget._Release;
        end;
    except
    end;
  Dispose(PtrGUID);
end;

 


Related Articles and Replies:



[ DelphiLand: free Delphi source code, tips, tutorials ]