Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Сети";
Текущий архив: 2004.05.23;
Скачать: [xml.tar.bz2];

Вниз

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;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.46 MB
Время: 0.03 c
8-1078229290
gagarin
2004-03-02 15:08
2004.05.23
эффекты DirectX


4-1081075528
Kme
2004-04-04 14:45
2004.05.23
Навигация в консоли


14-1083237452
SPeller
2004-04-29 15:17
2004.05.23
Как заработать денег?


3-1082552327
Галинка
2004-04-21 16:58
2004.05.23
Помогите кто чем может (Access)


6-1080943879
lena19
2004-04-03 02:11
2004.05.23
отключение от сервера





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский