Delphi Dünyası Facebook'ta

Kodbank İndir

! CODEBANK 2012 !

İNDİRMEK&DETAYLI BİLGİ ALMAK İÇİN BURAYI TIKLAYINIZ.

Gönderen Konu: html içinde arama yaptırmak  (Okunma sayısı 1553 defa)

0 Üye ve 1 Ziyaretçi konuyu incelemekte.

muderless

  • Ziyaretçi
html içinde arama yaptırmak
« : 26 Kasım 2005 01:44:42 »
Kod:  (Unknown Language)
  1. (*******************************************************
  2.  TheLiandRi/DeveloperX
  3.  24.11.2004
  4. ********************************************************)
  5.  
  6. unit Unit1;
  7.  
  8. interface
  9.  
  10. uses
  11.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  12.   Dialogs, OleCtrls, SHDocVw, MShtml,StdCtrls;
  13.  
  14. type
  15.   TForm1 = class(TForm)
  16.     btnAra: TButton;
  17.     edSearchText: TEdit;
  18.     WebBrowser1: TWebBrowser;
  19.     Label1: TLabel;
  20.     edDIR: TEdit;
  21.     Label2: TLabel;
  22.     Label3: TLabel;
  23.     lbURL: TLabel;
  24.     Label4: TLabel;
  25.     Label5: TLabel;
  26.     function SearchHtml(Aranan:String):string;
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure FormDestroy(Sender: TObject);
  29.     procedure GetHTMLFileList;
  30.     procedure btnAraClick(Sender: TObject);
  31.     procedure SearchAndHighlightText(aText: string);
  32.     procedure WebBrowser1DocumentComplete(Sender: TObject;
  33.       const pDisp: IDispatch; var URL: OleVariant);
  34.   private
  35.     { Private declarations }
  36.   public
  37.     FileList:TStringList;
  38.  
  39.   end;
  40.  
  41. var
  42.   Form1: TForm1;
  43.  
  44. implementation
  45.  
  46. {$R *.dfm}
  47.  
  48. function TForm1.SearchHtml(Aranan:String):string;
  49. var
  50.   strl:TStringList;
  51.   i:integer;
  52. begin
  53.   Result:='';
  54.   strl:=TStringList.Create;
  55.   for i:=0 to FileList.Count-1 do
  56.   begin
  57.    strl.LoadFromFile(FileList.Strings[i]);
  58.    if Pos(Aranan,strl.Text)>0 then     // aranan text dosyada varmı
  59.    begin
  60.      Result:=FileList.Strings[i];   // varsa dosyanın adını al
  61.      Break;                         // ve işlemi durdur
  62.    end;
  63.   end;
  64.   strl.Free;
  65. end;
  66.  
  67.  
  68. procedure TForm1.FormCreate(Sender: TObject);
  69. begin
  70.   FileList:=TStringList.Create;
  71.   edDIR.Text:=ExtractFilePath(Application.ExeName)+'html\';
  72.  
  73.   ///html dosyalarını bul
  74.   GetHTMLFileList;
  75. end;
  76.  
  77. procedure TForm1.FormDestroy(Sender: TObject);
  78. begin
  79.   FileList.Free;
  80. end;
  81.  
  82. procedure TForm1.GetHTMLFileList;
  83. var
  84.   myDir: TSearchRec;
  85.   c : Integer;
  86. begin
  87.    /// eğer dosya uzantıları .html ise burayı değiştir
  88.   c := FindFirst(edDIR.Text+'*.htm', FaAnyfile, myDir); // dizindeki .htm dosyalarını
  89.   while c = 0 do
  90.   begin
  91.     FileList.Add(edDIR.Text+myDir.Name); /// filelistemize ekliyoruz
  92.     c := FindNext(myDir);
  93.   end;
  94.   FindClose(myDir);
  95. end;
  96.  
  97. procedure TForm1.SearchAndHighlightText(aText: string);
  98. var
  99. tr: IHTMLTxtRange;
  100. begin
  101.   if not WebBrowser1.Busy then
  102.   begin
  103.     tr := ((WebBrowser1.Document as IHTMLDocument2).body as IHTMLBodyElement).createTextRange;
  104.     while tr.findText(aText, 1, 0) do
  105.     begin
  106.       tr.pasteHTML('<span style="background-color: Lime; font-weight: bolder;">' +
  107.         tr.htmlText + '</span>');
  108.       tr.scrollIntoView(True);
  109.     end;
  110.   end;
  111. end;
  112.  
  113. procedure TForm1.btnAraClick(Sender: TObject);
  114. var
  115.  nFile:string;
  116. begin
  117.  if edSearchText.Text<>'' then
  118.  begin
  119.    nFile:=SearchHtml(edSearchText.Text);
  120.    if nFile<>'' then
  121.    begin
  122.      lbURL.Caption:=ExtractFileName(nFile);
  123.      WebBrowser1.Navigate('file:///'+nFile);
  124.    end;
  125.  end
  126.  else
  127.    ShowMessage('Aranacak texti giriniz!');
  128. end;
  129.  
  130. procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  131.   const pDisp: IDispatch; var URL: OleVariant);
  132. begin
  133.  try
  134.   SearchAndHighlightText(edSearchText.Text);
  135.  except
  136.  end;
  137. end;
  138.  
  139. end.
« Son Düzenleme: 19 Haziran 2009 02:16:06 by Kocaturk »