Delphi 网络浏览器 scrollIntoView(true) 不工作

Delphi Webbrowser scrollIntoView(true) not working

我正在尝试在 Delphi 2010 年的网络浏览器中查找文本并滚动到其中。代码找到文本并滚动到它,但文本停留在 web 视图的底部(最后一行)。我想在网络视图的顶部(第一行)显示文本。

我想这个代码应该是 "scrollIntoView(true)",但它根本不会影响我正在尝试做的事情。

我该怎么办?谢谢。这是我的代码

procedure TForm1.SpeedButton10Click(Sender: TObject);
var
    doc: IHTMLDocument2;
    selection: IHTMLSelectionObject;
    textRange: IHtmlTxtRange;
    scrollpos: Integer;
    Art : string;
begin

Doc := WebBrowser1.Document as IHTMLDocument2;
Selection := Doc.Selection;
TextRange := selection.createRange as IHTMLTxtRange;

Art := edit2.Text;

TextRange.collapse(false);
if TextRange.findText(Art) then
begin
TextRange.select;
TextRange.scrollIntoView(true);

 end;
end;

我也无法 TextRange.scrollIntoView(True) 工作。但是,下面的代码似乎适用于

格式的文档

Line1
Line2
Line3
Line4
Line5
...
Line 100

前提是文档未滚动到 Line100 位于浏览器底线上方 window 的位置。它工作正常,f.i。,找到 Line20 并将其定位在浏览器的顶部 window。

如您所见,它通过从 TextRange 获取 IHTMLTextRangeMetrics 界面并使用其 offsetTop 属性 滚动 doc2 来工作父 window 垂直。

代码:

//  doc2 is a field of Form1 of type `IHTMLDocument2`
procedure TForm1.FindText(Text : String);
var
  selection: IHTMLSelectionObject;
  textRange: IHtmlTxtRange;
  scrollpos: Integer;
  Metrics : IHTMLTextRangeMetrics;
begin

  Selection := Doc2.Selection;
  TextRange := selection.createRange as IHTMLTxtRange;

  TextRange.collapse(false);
  if TextRange.findText(Text, 1, 0) then begin
    TextRange.select;
    TextRange.scrollIntoView(True);
    TextRange.QueryInterface(IHTMLTextRangeMetrics, Metrics);
    if Metrics <> Nil then
      doc2.parentWindow.scrollBy(0, Metrics.offsetTop);
  end;
end;