Главная страница
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.48 MB
Время: 0.063 c
4-1080651544
BorH
2004-03-30 16:59
2004.05.23
Список Uninstall


4-1078208915
Dark
2004-03-02 09:28
2004.05.23
Копирование каталогов


1-1084208904
ArthurKhamitov
2004-05-10 21:08
2004.05.23
CD-ROM


14-1082550292
WebErr
2004-04-21 16:24
2004.05.23
private vs protected


9-1074022239
zxc
2004-01-13 22:30
2004.05.23
несколько вопросов.