Главная страница
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.034 c
6-1080736606
Игорь
2004-03-31 16:36
2004.05.23
WebBrowser: поиск текста


3-1082740421
Frozzen
2004-04-23 21:13
2004.05.23
Заголовок колонки DBGrid


11-1071742891
BaDeVlad
2003-12-18 13:21
2004.05.23
Как в RichEdit изменить WordWrap?


14-1083775554
Maxim Vetera
2004-05-05 20:45
2004.05.23
XML


4-1081416983
Игорь
2004-04-08 13:36
2004.05.23
Помогите разобраться с Drag&amp;Drop