Главная страница
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.027 c
3-1082894259
Николай
2004-04-25 15:57
2004.05.23
Люди, подскажите запрос!


4-1081139547
juiceman
2004-04-05 08:32
2004.05.23
Хук на клавиатуру и мышь


3-1083366101
Mister
2004-05-01 03:01
2004.05.23
проблема соединения


9-1073465597
mad0max
2004-01-07 11:53
2004.05.23
GLScene - Дырка :)


14-1083601354
Thor
2004-05-03 20:22
2004.05.23
Слава Билли, слава его программистам!