【问题标题】:Delphi Webbrowser scrollIntoView(true) not workingDelphi Webbrowser scrollIntoView(true)不起作用
【发布时间】:2016-10-14 18:26:58
【问题描述】:

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

我认为此代码将是“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;

【问题讨论】:

    标签: delphi scroll twebbrowser


    【解决方案1】:

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

    第 1 行
    2号线
    3号线
    4号线
    5号线
    ...
    100 号线

    如果文档没有滚动到Line100 位于浏览器窗口底线上方的位置。它工作正常,f.i.,找到Line20 并将其放置在浏览器窗口的顶部。

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

    代码:

    //  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;
    

    【讨论】:

    • 非常感谢!我不知道为什么,它只有在代码执行两次时才有效。但它有效!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2020-01-09
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多