Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2004.05.23;
Скачать: CL | DM;

Вниз

WebBrowser: поиск текста   Найти похожие ветки 

 
Игорь   (2004-03-31 16:36) [0]

День добрый!

Подскажите, как правильно реализовать поиск текста в TWebBrowser.
Приведенный код на DelphiWorld работает через раз и не всегда верно:

{....}

 private
   procedure SearchAndHighlightText(aText: string);
   
{....}

procedure TForm1.SearchAndHighlightText(aText: string);
var
 i: Integer;
begin
 for i := 0 to WebBrowser1.OleObject.Document.All.Length - 1 do
 begin
   if Pos(aText, WebBrowser1.OleObject.Document.All.Item(i).InnerText) <> 0 then
   begin
     WebBrowser1.OleObject.Document.All.Item(i).Style.Color := "#FFFF00";
     WebBrowser1.OleObject.Document.All.Item(i).ScrollIntoView(True);
   end;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 SearchAndHighlightText("some text...");
end;

С уважением, Игорь.


 
VMcL ©   (2004-03-31 17:47) [1]

Что именно не работает?


 
int64   (2004-03-31 18:05) [2]

procedure TForm1.Button1Click(Sender: TObject);
var
 Range: IHTMLTxtRange;
 i: integer;
 FindText: WideString;
begin
 Range := HTMLBody(HTMLDocument(WB.Document).Body).createTextRange;
 i := 0;
 FindText := "s";
 while (Range.findText(FindText, i, 0) = true) do
   begin
     Range.select;
     Range.collapse(false);
     Application.ProcessMessages;
     sleep(1000);
     inc(i);
   end;
end;


 
nikkie ©   (2004-04-01 18:42) [3]

>[2]
во избежание memory leak надо заменить WB.Document на WB.DefaultInterface.Document, например.

вообще-то меня удивило, что пример работает. MSDN на эту тему пишет такое:

bFound = TextRange.findText(sText [, iSearchScope]  [, iFlags])
...
iSearchScope Optional. Integer that specifies the number of characters to search from the starting point of the range. A positive integer indicates a forward search; a negative integer indicates a backward search.
...
The value passed for the iSearchScope parameter controls the part of the document, relative to the range, that is searched. The behavior of the findText method depends on whether the state is degenerate or nondegenerate:

If the range is degenerate, passing a large positive number causes the text to the right of the range to be searched. Passing a large negative number causes the text to the left of the range to be searched.
If the range is nondegenerate, passing a large positive number causes the text to the right of the start of the range to be searched. Passing a large negative number causes the text to the left of the end of the range to be searched. Passing 0 causes only the text selected by the range to be searched.


в соответствии с этими объяснениями вместо i надо передавать "большое целое число", а inc(i) смысла не имеет.

PS лично мне больше нравится использование IHTMLBody вместо HTMLBody и HTMLDocument вместо IHTMLDocument. иначе какая-то странная смесь получается - Range-то объявлен как IHTMLTxtRange.



Страницы: 1 вся ветка

Текущий архив: 2004.05.23;
Скачать: CL | DM;

Наверх




Память: 0.47 MB
Время: 0.102 c
3-1082723996
Makaron
2004-04-23 16:39
2004.05.23
Обработка значений в QRDBText


14-1083323999
AlexKniga
2004-04-30 15:19
2004.05.23
Про катушку ниток.


14-1083379366
Думкин
2004-05-01 06:42
2004.05.23
С днем рождения! 1 мая


14-1083503615
Piter
2004-05-02 17:13
2004.05.23
Что такое GPF?


6-1081331309
NorthMan
2004-04-07 13:48
2004.05.23
Народ, в сети гуляет пример реализации PING а в Delphi